我有一个类型,t
我想获得具有该属性的公共属性列表MyAttribute
.该属性标有AllowMultiple = false
,如下所示:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
Run Code Online (Sandbox Code Playgroud)
目前我拥有的是这个,但我认为有更好的方法:
foreach (PropertyInfo prop in t.GetProperties())
{
object[] attributes = prop.GetCustomAttributes(typeof(MyAttribute), true);
if (attributes.Length == 1)
{
//Property with my custom attribute
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能改善这个?我很抱歉,如果这是重复的,那里有大量的反思线程......似乎这是一个非常热门的话题.