C# reflection avoid propertie
Im using reflection to acess a class tha represents a table in DB.However,reflection read all properties of that class,and im wondering if there're some atributte in c# we can 开发者_Python百科use to avoid read that propertie.
i.e:
[AvoidThisPropertie]
public string Identity
{
get;
set;
}
PropertyInfo [] properties = MyType.GetProperties(
BindingFlags.Instance | BindingFlags.Public);
IList<PropertyInfo> crawlableProperties = properties.Where(
p => p.GetCustomAttributes(
typeof(AvoidThisProperty), true)
.Count() == 0);
You'd also have to create the AvoidThisProperty
[AttributeUsage(AttributeTargets.Property)]
public class AvoidThisPropertyAttribute : Attribute
{
// Doesn't need a body
}
You still have access to all the properties, but the LINQ statement would generate a list of the desired properties.
If you could avoid full accessibility, reflection would have no sense
精彩评论