Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#反射避免属性_C#_Reflection - Fatal编程技术网

C#反射避免属性

C#反射避免属性,c#,reflection,C#,Reflection,我使用反射来访问一个在DB中表示一个表的类。但是,反射读取该类的所有属性,我想知道c#中是否有一些属性可以用来避免读取该属性 i、 e: 如果可以避免完全可访问性,反射就没有意义如果可以避免完全可访问性,反射就没有意义 PropertyInfo [] properties = MyType.GetProperties( BindingFlags.Instance | BindingFlags.Public); IList<PropertyInfo> crawlablePro

我使用反射来访问一个在DB中表示一个表的类。但是,反射读取该类的所有属性,我想知道c#中是否有一些属性可以用来避免读取该属性

i、 e:


如果可以避免完全可访问性,反射就没有意义

如果可以避免完全可访问性,反射就没有意义

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
}