我有一个这个代码的功能:
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;
但是,它不喜欢我!
哪一种更好的方法呢?
解决方法
我在想你可能想要检查接口的属性实现的类型. (删除冗余接口,IList继承ICollection和ICollection继承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); } }
我添加了不拒绝字符串的代码,因为它也实现了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); } }
原文地址:https://www.jb51.cc/csharp/94942.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。