【发布时间】:2010-09-20 02:10:18
【问题描述】:
我有一个接口,它定义了一些带有属性的方法。这些属性需要从调用方法中访问,但是我有的方法并没有从接口中拉取属性。我错过了什么?
public class SomeClass: ISomeInterface
{
MyAttribute GetAttribute()
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
object[] attributes = methodBase.GetCustomAttributes(typeof(MyAttribute), true);
if (attributes.Count() == 0)
throw new Exception("could not find MyAttribute defined for " + methodBase.Name);
return attributes[0] as MyAttribute;
}
void DoSomething()
{
MyAttribute ma = GetAttribute();
string s = ma.SomeProperty;
}
}
【问题讨论】:
-
只是检查一下,您已经在属性上设置了适当的标志以允许它被继承,不是吗?
标签: c# reflection attributes interface