I have a generic method that accepts a Func<int>
and I would like to log the Func that is passed into the method. What properties are available on the passed in func to hel开发者_如何学Cp me understand what it is doing?
You can log the method name with func.Method.Name
, and there's some other useful properties in the MethodInfo
class. However, if the Func
is anonymous, then you will not get a very helpful name.
As Jaroslav Jandek pointed out by using Expression<Func<T>>
I can get more information about the method being passed in. Specifically: I found that the body
property has the anonymous method signature as a string.
http://msdn.microsoft.com/en-us/library/system.linq.expressions.lambdaexpression.body.aspx
精彩评论