C#反射避免属性
我使用反射来访问一个在DB中表示一个表的类。但是,反射读取该类的所有属性,我想知道c#中是否有一些属性可以用来避免读取该属性 i、 e:C#反射避免属性,c#,reflection,C#,Reflection,我使用反射来访问一个在DB中表示一个表的类。但是,反射读取该类的所有属性,我想知道c#中是否有一些属性可以用来避免读取该属性 i、 e: 如果可以避免完全可访问性,反射就没有意义如果可以避免完全可访问性,反射就没有意义 PropertyInfo [] properties = MyType.GetProperties( BindingFlags.Instance | BindingFlags.Public); IList<PropertyInfo> crawlablePro
如果可以避免完全可访问性,反射就没有意义如果可以避免完全可访问性,反射就没有意义
PropertyInfo [] properties = MyType.GetProperties(
BindingFlags.Instance | BindingFlags.Public);
IList<PropertyInfo> crawlableProperties = properties.Where(
p => p.GetCustomAttributes(
typeof(AvoidThisProperty), true)
.Count() == 0);
您仍然可以访问所有属性,但LINQ语句将生成所需属性的列表
您仍然可以访问所有属性,但LINQ语句将生成所需属性的列表。定义“避免读取”。你的意思是说你根本不希望像GetProperties
这样的方法找到属性吗?或者要避免在发现属性值后读取该属性值?请定义“避免读取”。你的意思是说你根本不希望像GetProperties
这样的方法找到属性吗?还是要避免在发现属性值后读取它?
PropertyInfo [] properties = MyType.GetProperties(
BindingFlags.Instance | BindingFlags.Public);
IList<PropertyInfo> crawlableProperties = properties.Where(
p => p.GetCustomAttributes(
typeof(AvoidThisProperty), true)
.Count() == 0);
[AttributeUsage(AttributeTargets.Property)]
public class AvoidThisPropertyAttribute : Attribute
{
// Doesn't need a body
}