【转】EF 获取类的属性并排除特定属性(getType().GetProperties())

当获取一个类型(class)的所有属性时,想排除指定属性,该如何操作?

比如:EF中一个实体类型UserEntity,通过反射获取这个类的属性时,想排除这个为映射的字段ID

使用以下方法即可!

PropertyInfo[] props =entity.GetType().GetProperties().Where(v => v.GetCustomAttributes(typeof(NotMappedAttribute), true).Length == 0).ToArray();//排除未映射的字段
//更优雅的方法
PropertyInfo[] props = entity.GetType().GetProperties().Where(pi => !Attribute.IsDefined(pi, typeof(NotMappedAttribute))).ToArray();//排除未映射字段

 参考:http://stackoverflow.com/questions/2051834/exclude-property-from-gettype-getproperties

转自:https://www.cnblogs.com/miralce/archive/2017/01/18/6297595.html

原文地址:https://www.cnblogs.com/hycms/p/9123733.html