Bazı yöntemlerin özniteliklerle tanımlandığı bir arabirim var. Bu özniteliklerin çağrı yönteminden erişilmesi gerekir, ancak sahip olduğum yöntem, öznitelikleri arabirimden çekmiyor. Neyi kaçırıyorum?Bir arabirimdeki özellikler
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;
}
}
Yalnızca bir çeke, miras almanıza izin vermek için özniteliğinize uygun bayrağı ayarladınız değil mi? –