如何识别通用声明类型?

sgt*_*gtz 2 c# reflection

假设您已将IList或List作为属性.你怎么知道它是List还是IList?这可以在不依赖反复试验的情况下完成吗?

类型的名称往往类似于List`1.考虑字符串黑客是否合理?

class Program {

    public class Class1 {
        public int a { get; set; }

        public IList<int> list { get; set; }


        public List<int> concreteList { get; set; }
    }

    static void Main(string[] args)
    {
        Test1();
        Test2();
    }

    private static void Test1()
    {
        var t = typeof (Class1);
        var p = t.GetProperty("list");

        if (p.PropertyType.IsInterface && p.PropertyType.IsGenericType)
        {
            var ps = p.PropertyType.GetGenericArguments();
            var underlying = p.PropertyType.GetInterface("IList");

            var b = underlying == typeof (IList<>);

        }
    }

    private static void Test2() {
        var t = typeof(Class1);
        var p = t.GetProperty("concreteList");

        if (!p.PropertyType.IsInterface && p.PropertyType.IsGenericType) {
            var ps = p.PropertyType.GetGenericArguments();

            var underlying3 = p.PropertyType.GetGenericTypeDefinition();

            var b = underlying3 == typeof (List<>);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*ayC 5

如果你能得到属性值,那么测试它的类型可能非常简单(参见Guffa的回答).但是,如果你想在不调用属性的情况下找到它,那么你的代码几乎就在那里 - 例如,

var t = p.PropertyType;
if (t.IsGenericType && !t.IsGenericTypeDefinition && !t.IsInterface && !t.IsValueType) 
{
   // we are dealing with closed generic classes 
   var typeToTest = typeof (List<>);
   var tToCheck = t.GetGenericTypeDefinition();
   while (tToCheck != typeof(object)) 
   {
      if (tToCheck == typeToTest) 
      {
         // the given type is indeed derived from List<T>
         break; 
      }
      tToCheck = toCheck.BaseType;
   }
}
Run Code Online (Sandbox Code Playgroud)

IsGenericType表示类型是通用的 - 可以是open(List<T>)或closed(List<int>).IsGenericTypeDefinition指示泛型类型是否为开放类型.GetGenericTypeDefinitionon closed/open泛型类型将返回泛型定义(即开放泛型类型).