在类和方法定义中,可以添加类型约束where T : IFoo.
是否有可能用System.Type或反映这些约束MethodInfo?到目前为止我还没有找到任何东西; 任何帮助将不胜感激.
我想知道为什么IEnumerable<int>不能分配到IEnumerable<object>.毕竟IEnumerable是支持协方差的少数接口之一......
int 似乎是一个适当的亚型 object两种功能的组合不起作用......
class A
{
}
class B : A
{
}
class Program
{
static void Main(string[] args)
{
bool b;
b = typeof(IEnumerable<A>).IsAssignableFrom(typeof(List<B>));
Console.WriteLine("ienumerable of ref types is covariant: " + b); //true
b = typeof(IEnumerable<object>).IsAssignableFrom(typeof(List<int>));
Console.WriteLine("ienumerable of value tpyes is covariant: " + b); //false
b = typeof(object).IsAssignableFrom(typeof(int));
Console.WriteLine("int is a subtype of object: " + b); //true
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!塞巴斯蒂安