如何知道propertyInfo是否为C#中的IList类型?
鉴于这一类别:如何知道propertyInfo是否为C#中的IList类型?,c#,reflection,C#,Reflection,鉴于这一类别: public class SomeClass { public int SomeProperty { get; set; } public IList<AnotherClass> MyList { get; set; } } 公共类SomeClass { 公共int SomeProperty{get;set;} 公共IList MyList{get;set;} } 该代码: SomeClass myClass = new SomeClass();
public class SomeClass
{
public int SomeProperty { get; set; }
public IList<AnotherClass> MyList { get; set; }
}
公共类SomeClass
{
公共int SomeProperty{get;set;}
公共IList MyList{get;set;}
}
该代码:
SomeClass myClass = new SomeClass();
PropertyInfo[] properties = myClass.GetType().GetProperties();
for(int i = 0; i < properties.Length; i++)
{
//How can I figure that the current property is a collection/list?
}
SomeClass myClass=新的SomeClass();
PropertyInfo[]properties=myClass.GetType().GetProperties();
for(int i=0;i
我尝试过的事情:
bool a1 = properties[i].PropertyType.IsAssignableFrom(typeof(IList));
//false
bool a2 = properties[i].PropertyType.IsAssignableFrom(typeof(IList<>));
//false
bool a3 = typeof(IList).IsAssignableFrom(properties[i].PropertyType);
//false
bool a4 = typeof(IList<>).IsAssignableFrom(properties[i].PropertyType);
//false
bool a5 = properties[i].PropertyType.Equals(typeof(IList));
//false
bool a6 = properties[i].PropertyType.Equals(typeof(IList<>));
//false
bool a7 = properties[i].PropertyType.IsSubclassOf(typeof(IList));
//false
bool a8 = properties[i].PropertyType.IsSubclassOf(typeof(IList<>));
//false
bool a9 = properties[i].PropertyType is IList;
//false
bool a0 = typeof(ICollection<>).IsAssignableFrom(properties[i].PropertyType);
//false
boola1=properties[i].PropertyType.IsAssignableFrom(typeof(IList));
//假的
boola2=properties[i].PropertyType.IsAssignableFrom(typeof(IList));
//假的
bool a3=typeof(IList).IsAssignableFrom(properties[i].PropertyType);
//假的
bool a4=typeof(IList).IsAssignableFrom(properties[i].PropertyType);
//假的
boola5=properties[i].PropertyType.Equals(typeof(IList));
//假的
bool a6=properties[i].PropertyType.Equals(typeof(IList));
//假的
bool a7=properties[i].PropertyType.IsSubclassOf(typeof(IList));
//假的
bool a8=properties[i].PropertyType.IsSubclassOf(typeof(IList));
//假的
bool a9=属性[i]。属性类型为IList;
//假的
bool a0=typeof(ICollection).IsAssignableFrom(properties[i].PropertyType);
//假的
用PropertyType.GetType()
加上以上所有内容。我怎么才能知道呢?你可以使用
if(属性[i].PropertyType.IsGenericType&&
属性[i].PropertyType.GetGenericTypeDefinition()==typeof(IList))
对于所有IList
类型,这将返回true。如果要检查其他类型(ICollection
,IEnumerable
等),也可以对它们执行相同的检查。控制台.WriteLine(“Is IList:+(new List()).GetType().GetInterfaces().Any(i=>i.IsGenericType&&i.GetGenericTypeDefinition()==typeof(IList))
从这个问题:您可以使用System.Collection.IList变体:
public static bool IsIList(this Type type)
{
return type.GetInterfaces().Contains(typeof(System.Collections.IList));
}
我注意到,在一些示例中,您正在向后使用
IsAssignableFrom
。a3
应该返回true。但是,依我看,对于具有不同属性的类,这将失败GetGenericTypeDefinition(..)
只能在泛型类型属性上调用,否则将引发异常。需要安全检查。@Tigran你说得对,谢谢你指出这一点。我添加了检查以确保类型是泛型的。
public static bool IsIList(this Type type)
{
return type.GetInterfaces().Contains(typeof(System.Collections.IList));
}