2009-08-04 67 views
2

我想获取作为Func委托的方法的名称。检索在Func中执行的调用方法的名称

Func<MyObject, object> func = x => x.DoSomeMethod(); 
string name = ExtractMethodName(func); // should equal "DoSomeMethod" 

我该如何做到这一点?

- 吹牛的权利 -

ExtractMethodName也有一个属性调用工作,有它在该实例返回属性名称。

例如。

Func<MyObject, object> func = x => x.Property; 
string name = ExtractMethodName(func); // should equal "Property" 

回答

11

Look Ma!没有表情树!

下面是一个快速,肮脏且特定于实现的版本,它从基础lambda的IL流抓取元数据令牌并解析它。

private static string ExtractMethodName(Func<MyObject, object> func) 
{ 
    var il = func.Method.GetMethodBody().GetILAsByteArray(); 

    // first byte is ldarg.0 
    // second byte is callvirt 
    // next four bytes are the MethodDef token 
    var mdToken = (il[5] << 24) | (il[4] << 16) | (il[3] << 8) | il[2]; 
    var innerMethod = func.Method.Module.ResolveMethod(mdToken); 

    // Check to see if this is a property getter and grab property if it is... 
    if (innerMethod.IsSpecialName && innerMethod.Name.StartsWith("get_")) 
    { 
     var prop = (from p in innerMethod.DeclaringType.GetProperties() 
        where p.GetGetMethod() == innerMethod 
        select p).FirstOrDefault(); 
     if (prop != null) 
      return prop.Name; 
    } 

    return innerMethod.Name; 
} 
+0

谢谢!我没有想到事情会变得如此怪异,但它可以按照需要运行。 – berko 2009-08-04 06:38:53

0

我不认为这在一般情况下是可能的。如果你有:

Func<MyObject, object> func = x => x.DoSomeMethod(x.DoSomeOtherMethod()); 

你会期待什么?这就是说,你可以使用反射来打开Func对象,看看它在里面做什么,但你只能在某些情况下解决它。

+0

鉴于你有我想要的答案是“DoSomeMethod”。 我提供的属性示例有可能更容易,因为这会限制Func询问的潜在复杂性? – berko 2009-08-04 04:06:22

0

看看我砍的答案在这里:

Why is there not a `fieldof` or `methodof` operator in C#?

在过去,我做到了另一种方式是使用Func代替Expression<Func<...>>,但我为这个结果高兴的要少得多。当使用属性时,用于检测我的fieldof方法中的字段的MemberExpression将返回PropertyInfo

编辑#1:这适用于该问题的一个子集:

Func<object> func = x.DoSomething; 
string name = func.Method.Name; 

编辑#2:谁标志着我失望应采取第二意识到这里发生了什么上。表达式树可以隐式与lambda表达式一起使用,并且是获取特定请求信息的最快,最可靠的方式。

相关问题