Answer the question
In order to leave comments, you need to log in
Getting method attributes when using Func?
Hello.
There is a c# code that does not work as we would like :)
public class myclass
{
public class MyAttribute : Attribute { }
[MyAttribute]
public string MyMethod()
{
return "";
}
public void MainMethod()
{
Func<string> m = MyMethod;
Type t = m.GetType();
object[] attrs=t.GetCustomAttributes(typeof(MyAttribute), true);
}
}
Answer the question
In order to leave comments, you need to log in
public void MainMethod()
{
Func<string> method = MyMethod;
var methodName = method.GetMethodInfo().Name;
var type = method.Target.GetType();
var attrs = type.GetMethod(methodName).GetCustomAttributes(typeof(MyAttribute), true);
}
public void MainMethod()
{
var method = typeof(myclass).GetMethod("MyMethod");
var attrs = method.GetCustomAttributes(typeof(MyAttribute), true);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question