如何定义原始类型的泛型类型限制?

时间:2022-02-10 16:36:02

I have the following method with generic type:

我有以下方法与泛型类型:

T GetValue<T>();

I would like to limit T to primitive types such as int, string, float but not class type. I know I can define generic for class type like this:

我想将T限制为原始类型,如int,string,float,但不是类类型。我知道我可以像这样为类类定义泛型:

C GetObject<C>() where C: class;

I am not sure if it is possible for primitive types and how if so.

我不确定原始类型是否可能以及如果可能。

5 个解决方案

#1


42  

You can use this to limit it to value types:

您可以使用它将其限制为值类型:

where C: struct

You also mention string. Unfortunately, strings won't be allowed as they are not value types.

你还提到了字符串。不幸的是,不允许字符串,因为它们不是值类型。

#2


15  

Actually this does the job to certain extend:

实际上,这在某种程度上完成了工作:

public T Object<T>() where T :
   struct, IComparable, IFormattable, IConvertible, IComparable<T>, IEquatable<T>

To limit to numeric types you can get some useful hints of the following samples defined for the ValueType class

要限制数值类型,您可以获得为ValueType类定义的以下示例的一些有用提示

#3


13  

Here's what you're looking for:

这是你要找的东西:

T GetObject<T>() where T : struct;

#4


9  

There is no generic constraint that matches that set of things cleanly. What is it that you actually want to do? For example, you can hack around it with runtime checks, such as a static ctor (for generic types - not so easy for generic methods)...

没有通用约束可以干净地匹配这组事物。你真的想做什么?例如,您可以通过运行时检查来解决它,例如静态ctor(对于泛型类型 - 对于泛型方法来说不那么容易)......

However; most times I see this, it is because people want one of:

然而;大多数时候我看到这一点,这是因为人们想要一个:

  • to be able to check items for equality: in which case use EqualityComparer<T>.Default
  • 能够检查项目是否相等:在这种情况下使用EqualityComparer .Default
  • to be able to compare/sort items: in which case use Comparer<T>.Default
  • 能够比较/排序项目:在这种情况下使用Comparer .Default
  • to be able to perform arithmetic: in which case use MiscUtil's support for generic operators
  • 能够执行算术:在这种情况下使用MiscUtil支持通用运算符

#5


5  

What are you actually trying to do in the method? It could be that you actually need C to implement IComparable, or someother interface. In which case you want something like

你在这个方法中实际上想做什么?可能是您实际需要C来实现IComparable或其他一些接口。在这种情况下你想要的东西

T GetObject<T> where T: IComparable

#1


42  

You can use this to limit it to value types:

您可以使用它将其限制为值类型:

where C: struct

You also mention string. Unfortunately, strings won't be allowed as they are not value types.

你还提到了字符串。不幸的是,不允许字符串,因为它们不是值类型。

#2


15  

Actually this does the job to certain extend:

实际上,这在某种程度上完成了工作:

public T Object<T>() where T :
   struct, IComparable, IFormattable, IConvertible, IComparable<T>, IEquatable<T>

To limit to numeric types you can get some useful hints of the following samples defined for the ValueType class

要限制数值类型,您可以获得为ValueType类定义的以下示例的一些有用提示

#3


13  

Here's what you're looking for:

这是你要找的东西:

T GetObject<T>() where T : struct;

#4


9  

There is no generic constraint that matches that set of things cleanly. What is it that you actually want to do? For example, you can hack around it with runtime checks, such as a static ctor (for generic types - not so easy for generic methods)...

没有通用约束可以干净地匹配这组事物。你真的想做什么?例如,您可以通过运行时检查来解决它,例如静态ctor(对于泛型类型 - 对于泛型方法来说不那么容易)......

However; most times I see this, it is because people want one of:

然而;大多数时候我看到这一点,这是因为人们想要一个:

  • to be able to check items for equality: in which case use EqualityComparer<T>.Default
  • 能够检查项目是否相等:在这种情况下使用EqualityComparer .Default
  • to be able to compare/sort items: in which case use Comparer<T>.Default
  • 能够比较/排序项目:在这种情况下使用Comparer .Default
  • to be able to perform arithmetic: in which case use MiscUtil's support for generic operators
  • 能够执行算术:在这种情况下使用MiscUtil支持通用运算符

#5


5  

What are you actually trying to do in the method? It could be that you actually need C to implement IComparable, or someother interface. In which case you want something like

你在这个方法中实际上想做什么?可能是您实际需要C来实现IComparable或其他一些接口。在这种情况下你想要的东西

T GetObject<T> where T: IComparable