Ari*_*ian 10 c# reflection attributes data-annotations c#-4.0
可能重复:
如何获取具有给定属性的属性列表?
我有这样的自定义类
public class ClassWithCustomAttributecs
{
[UseInReporte(Use=true)]
public int F1 { get; set; }
public string F2 { get; set; }
public bool F3 { get; set; }
public string F4 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个自定义属性UseInReporte
:
[System.AttributeUsage(System.AttributeTargets.Property ,AllowMultiple = true)]
public class UseInReporte : System.Attribute
{
public bool Use;
public UseInReporte()
{
Use = false;
}
}
Run Code Online (Sandbox Code Playgroud)
不,我想获得所有具有[UseInReporte(Use=true)]
如何使用反射做到这一点的属性?
谢谢
And*_*rei 18
List<PropertyInfo> result =
typeof(ClassWithCustomAttributecs)
.GetProperties()
.Where(
p =>
p.GetCustomAttributes(typeof(UseInReporte), true)
.Where(ca => ((UseInReporte)ca).Use)
.Any()
)
.ToList();
Run Code Online (Sandbox Code Playgroud)
当然typeof(ClassWithCustomAttributecs)
应该替换为您正在处理的实际对象.