C# 忽略PropertyInfo中的集合属性
我有一个具有以下代码的函数:C# 忽略PropertyInfo中的集合属性,c#,properties,propertyinfo,C#,Properties,Propertyinfo,我有一个具有以下代码的函数: foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()){ //SOME CODE if (propertyInfo.CanWrite) propertyInfo.SetValue(myCopy, propertyInfo.GetValue(obj, null), null); } 我会避免检查“收藏”属性;要执行此操作,我必须插入此控件: if (propertyInfo.Prop
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()){
//SOME CODE
if (propertyInfo.CanWrite)
propertyInfo.SetValue(myCopy, propertyInfo.GetValue(obj, null), null);
}
我会避免检查“收藏”属性;要执行此操作,我必须插入此控件:
if (propertyInfo.PropertyType.Name.Contains("List")
|| propertyInfo.PropertyType.Name.Contains("Enumerable")
|| propertyInfo.PropertyType.Name.Contains("Collection"))
continue;
但是,它不喜欢我
哪种方法更好?我可能会对照
IEnumerable
进行检查
if ((typeof(string) != propertyInfo.PropertyType)
&& typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
{
continue;
}
我想您可能需要检查属性实现类型的接口。(删除了冗余接口,因为IList继承ICollection,ICollection继承IEnumerable。)
static void DoSomething()
{
列表集合=新列表(){typeof(IEnumerable),typeof(IEnumerable)};
foreach(PropertyInfo PropertyInfo在typeof(T).GetProperties()中)
{
if(propertyInfo.PropertyType!=typeof(string)&&propertyInfo.PropertyType.GetInterfaces().Any(i=>collections.Any(c=>i==c)))
{
继续;
}
Console.WriteLine(propertyInfo.Name);
}
}
我添加了不拒绝字符串的代码,因为它也实现了IEnumerable,我想您可能希望保留这些代码
考虑到前面的集合接口列表的冗余性,这样编写代码可能更简单
static void DoSomething<T>()
{
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
if (propertyInfo.PropertyType != typeof(string)
&& propertyInfo.PropertyType.GetInterface(typeof(IEnumerable).Name) != null
&& propertyInfo.PropertyType.GetInterface(typeof(IEnumerable<>).Name) != null)
{
continue;
}
Console.WriteLine(propertyInfo.Name);
}
}
static void DoSomething()
{
foreach(PropertyInfo PropertyInfo在typeof(T).GetProperties()中)
{
if(propertyInfo.PropertyType!=typeof(字符串)
&&propertyInfo.PropertyType.GetInterface(typeof(IEnumerable.Name)!=null
&&propertyInfo.PropertyType.GetInterface(typeof(IEnumerable.Name)!=null)
{
继续;
}
Console.WriteLine(propertyInfo.Name);
}
}
噢,现在我有一个问题……这个条件对于字符串属性也是如此。可能吗?@LukePet:Strings实现IEnumerable
,因为它们是字符的集合。Anthony的答案明确排除了字符串,所以您选择这一个是正确的(我已经更新了我的答案以跟进)。是否还有其他实现IEnumerable的基本类型,如“string”?@LukePet:我不知道。IEnumerable实现IEnumerable,不需要检查IEnumerable
static void DoSomething<T>()
{
List<Type> collections = new List<Type>() { typeof(IEnumerable<>), typeof(IEnumerable) };
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
if (propertyInfo.PropertyType != typeof(string) && propertyInfo.PropertyType.GetInterfaces().Any(i => collections.Any(c => i == c)))
{
continue;
}
Console.WriteLine(propertyInfo.Name);
}
}
static void DoSomething<T>()
{
foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
if (propertyInfo.PropertyType != typeof(string)
&& propertyInfo.PropertyType.GetInterface(typeof(IEnumerable).Name) != null
&& propertyInfo.PropertyType.GetInterface(typeof(IEnumerable<>).Name) != null)
{
continue;
}
Console.WriteLine(propertyInfo.Name);
}
}