[英]Is it possible to define a “not Nullable<T>” constraint in a C# generic method?
在C#中, Nullable<T>
类型不满足where
struct
泛型约束(而AFAK这在技术上是一个结构)。 这可用于指定泛型参数必须是不可为空的值类型:
T DoSomething<T>() where T : struct
{
//...
}
DoSomething<int?>(); //not ok
DoSomething<int>(); //ok
当然, Nullable<T>
也不满足引用类型where
class
约束:
T DoSomething<T>() where T : class
{
//...
}
DoSomething<int?>(); //not ok
DoSomething<Foo>(); //ok
这是否可以定义约束,例如它必须是引用类型或值类型但不是Nullable值类型?
像这样的东西:
void DoSomething<T>() where T : class, struct //wont compile!
{
//...
}
DoSomething<int?>(); //not ok
DoSomething<int>(); //ok
DoSomething<Foo>(); //ok
如评论中所述,您可以使用重载和参数(可以是可选的)来执行此操作。 我刚刚在博客上写过这篇文章 ,但在你的情况下,你想要:
public class ClassConstraint<T> where T : class
{
}
public class SomeClass<TViewModel>
{
public void Add<TValue>(Func<TViewModel, TValue> expression,
ClassConstraint<TValue> ignored = null)
where TValue : class
{
AddImpl(expression);
}
public void Add<TValue>(Func<TViewModel, TValue> expression,
Nullable<TValue> ignored = null)
where TValue : struct
{
AddImpl(expression);
}
// No constraints
private void AddImpl<TValue>(Func<TViewModel, TValue> expression)
{
...
}
}
这很难看,但它有效:
var z = new SomeClass<string>();
z.Add(x => x.Length); // Valid (non-nullable value type)
z.Add(x => x); // Valid (reference type)
z.Add(x => new DateTime?()); // Invalid (nullable value type)
不,在宣言方面是不可能的。 它是struct
OR class
。 但是,您可以在运行时检查typeof(T)
以确保T
为Nullable<T2>
Type type = typeof(T);
if(Nullable.GetUnderlyingType(type) == null)
throw new Exception();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.