How I can get a custom attribute from a Function object?
public static void Main(string[] args) {
Func<object> fun = () => Foo();
//fun. Get Foo Custom Attribute
}
[CustomAttrib]
public static object Foo() {
return new object();
}
How I can get a custom attribute from a Function object?
public static void Main(string[] args) {
Func<object> fun = () => Foo();
//fun. Get Foo Custom Attribute
}
[CustomAttrib]
public static object Foo() {
return new object();
}
With a Function
, the information you're looking for is not available AFAIK.
However, if it were an Expression
, it would be quite easy:
Expression<Func<object>> expression = () => Foo();
var method = ((MethodCallExpression)expression.Body).Method;
Console.WriteLine(method.GetCustomAttributes(false)[0]);
You can convert an Expression
to a Function
with the Compile()
method.
var fun = expression.Compile();
This might be helpful if you define the Function
yourself, but not if comes from some 3rd party code which you can't modify.