小编RCP*_*CPT的帖子

访问自定义属性的值

我有这个自定义属性:

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited = true)]
class MethodTestingAttibute : Attribute
{   
    public string Value{ get; private set; }
    public MethodTestingAttibute (string value)
    {
        this.Value= value;

    }
}
Run Code Online (Sandbox Code Playgroud)

要像这样使用:

[MethodTestingAttibute("2")]
public int m1() {return 3; }
Run Code Online (Sandbox Code Playgroud)

而我的理念是采用MethodTestingAttibute的"2"值

object result = method.Invoke(obj, new Type[] {}); // here i get the return
Run Code Online (Sandbox Code Playgroud)

现在我想将此结果与Method TestingAttibute的值进行比较.我怎么能这样做?我试图走上这条路,但没有成功:method.GetCustomAttributes(typeof(MethodTestAttibute),true)[0] ...

什么是正确访问Custoum属性的字段?

c# attributes

22
推荐指数
2
解决办法
4万
查看次数

标签 统计

attributes ×1

c# ×1