【问题标题】:Read all methode attributes using reflection in C#在 C# 中使用反射读取所有方法属性
【发布时间】:2012-09-14 08:17:45
【问题描述】:

我正在尝试读取包含多个相同类型属性的方法的所有属性,例如:

[field(Feld = "x86", Index = 1)]
[field(Feld = "x93", Index = 2)]
...
[field(Feld = "x107", Index = 9)]
public string Methodename() {}

读取属性如下:

Attribute mAttr = Attribute.GetCustomAttribute
                  (methodInfo, typeof (fieldAttribute), false) as fieldAttribute;

这会抛出一个AmbiguousMatchException。如何读取多个属性?

谢谢

【问题讨论】:

    标签: c# exception reflection custom-attributes


    【解决方案1】:

    使用 GetCustomAttributes 而不是 GetCustomAttribute :)

    例如:

    Attribute[] attributes = Attribute.GetCustomAttributes
                  (methodInfo, typeof (fieldAttribute), false);
    

    【讨论】:

    • 天哪,我得瞎了眼!我什至没有在复数中寻找相同的方法名称。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多