相关疑难解决方法(0)

如何获取具有给定属性的属性列表?

我有一个类型,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)

我怎样才能改善这个?我很抱歉,如果这是重复的,那里有大量的反思线程......似乎这是一个非常热门的话题.

.net c# reflection

201
推荐指数
5
解决办法
10万
查看次数

标签 统计

.net ×1

c# ×1

reflection ×1