【发布时间】:2011-06-30 17:00:21
【问题描述】:
我有这个自定义属性:
[AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited = true)]
class MethodTestingAttibute : Attribute
{
public string Value{ get; private set; }
public MethodTestingAttibute (string value)
{
this.Value= value;
}
}
这样使用:
[MethodTestingAttibute("2")]
public int m1() {return 3; }
而我的困难是取MethodTestingAttibute的“2”的值
object result = method.Invoke(obj, new Type[] {}); // here i get the return
现在我想将此结果与MethodTestingAttibute 的值进行比较。我怎样才能做到这一点?我正在尝试走这条路但没有成功:
method.GetCustomAttributes(typeof(MethodTestAttibute), true)[0]...
访问自定义属性字段的正确方法是什么?
【问题讨论】:
-
我很困惑。你说的“3”是指“2”吗?
标签: c# attributes