简体   繁体   English

如何获取泛型函数的方法名<t>传入方法</t>

[英]How to get Method Name of Generic Func<T> passed into Method

I'm trying to get the method name of a function passed into an object using a.Net closure like this:我正在尝试使用 .Net 闭包将 function 的方法名称传递给 object,如下所示:

Method Signature方法签名

public IEnumerable<T> GetData<T>(Func<IEnumerable<T>> WebServiceCallback) 
where T : class    
{
    // either gets me '<LoadData>b__3'
    var a = nrdsWebServiceCallback.Method.Name;
    var b = nrdsWebServiceCallback.GetInvocationList();

    return WebServiceCallback();
}

I'm calling it like this:我这样称呼它:

SessionStateService.Labs = CacheManager.GetData(() =>  
WCFService.GetLabs(SessionStateService.var1, SessionStateService.var2));

Seeing 'b__3' instead of WCFServce.GetLabs(..) etc看到 'b__3' 而不是 WCFServce.GetLabs(..) 等

You're looking at the name of the lambda expression (generated by the compiler), instead of the name of the method called inside the lambda.您正在查看 lambda 表达式的名称(由编译器生成),而不是在 lambda 内部调用的方法的名称。

You have to use an <Expression<Func<T>> instead of a Func<T> .您必须使用<Expression<Func<T>>而不是Func<T> Expressions can be parsed and analyzed.可以解析和分析表达式。

Try尝试

public IEnumerable<T> GetData<T>(Expression<Func<IEnumerable<T>>> callbackExpression) 
where T : class    
{
    var methodCall = callbackExpression.Body as MethodCallExpression;
    if(methodCall != null)
    {
        string methodName = methodCall.Method.Name;
    }

    return callbackExpression.Compile()();
}

What is actually passed into your function is an anonymous lambda function ( () => WCFService.Etc ), so what you're seeing is the actual method name - <LoadData>b__3 is the autogenerated name for the anonymous method.实际传递给您的函数的是匿名 lambda 函数 ( () => WCFService.Etc ),因此您看到的是实际方法名称 - <LoadData>b__3是匿名方法的自动生成名称。

What you actually want is the method called inside the method called.你真正需要的是被称为调用的方法的方法。 For that, you need to delve into expressions.为此,您需要深入研究表达式。 Instead of Func<IEnumerable<T>> , define your parameter as Expression<Func<IEnumerable<T>>> , and call this code:而不是Func<IEnumerable<T>> ,将您的参数定义为Expression<Func<IEnumerable<T>>> ,并调用以下代码:

var body = nrdsWebServiceCallback.Body as MethodCallExpression;
if (body != null)
   Console.WriteLine(body.Method.DeclaringType.Name + "." + body.Method.Name);

Here is another workaround:这是另一种解决方法:

Because you are using a callback (an anonymous function), and this is the way that its name is represented in the .Method.Name property.因为您正在使用回调(匿名函数),这是其名称在.Method.Name属性中表示的.Method.Name

You can use RegEx to fetch the name of the method from it:您可以使用 RegEx 从中获取方法的名称:

/// <summary>
/// Gets a string between the characters '<' and '>' 
/// Which is the method name for anonymous methods.
/// </summary>
/// <param name="methodName"></param>
/// <returns>Simple method name</returns>
private static string GetMethodName(string methodName)
{
    var pattern = @"(?<=\<)(.*?)(?=\>)";
    var regex = new Regex(pattern);

    return regex.Match(methodName).Value;
}

It will take the string: <LoadData>b__3它将采用字符串: <LoadData>b__3

And return the name of the function: LoadData并返回函数名: LoadData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM