如何从字符串为Lambda表达式动态创建方法 [英] How to Dynamically Create Method from String for Lambda Expression

查看:122
本文介绍了如何从字符串为Lambda表达式动态创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是创建一个函数,该函数将方法名称动态传递给Hangfire库中的类.

My ultimate goal is to create a function that will dynamically pass method names to classes in the Hangfire library.

例如,这是有效的非动态代码:

For example, here is the non-dynamic code which works:

RecurringJob.AddOrUpdate(() => myFunction(), Cron.Hourly)

AddOrUpdate的第一个参数的类型为Expression<Action>.我的第一步是使用反射来动态插入函数名称:

The type of the first argument for AddOrUpdate is Expression<Action>. My first step was to use reflection to dynamically insert the function name:

Type thisControllerType = this.GetType();
MethodInfo method = thisControllerType.GetMethod(methodName); //methodName passed as string parameter
RecurringJob.AddOrUpdate(() => method.Invoke(this, null), Cron.Hourly);

当我检查Hangfire仪表板时,似乎此表达式被评估为MethodBase.Invoke.所以我需要动态传递方法名称的帮助.

When I check the Hangfire dashboard, it seems that this expression is being evaluated as MethodBase.Invoke. So I need help passing in the method name dynamically.

这可能足以回答我的问题,但是我采取的另一种方法是尝试为参数生成整个表达式.

That may be enough info to answer my question, but another path I have taken is trying to generate the entire expression for the argument.

RecurringJob.AddOrUpdate(CreateCallExpression(method), Cron.Hourly);

public Expression<Action> CreateCallExpression(MethodInfo method)
    {
        //trying to pass in zero argument parameter, not sure if this syntax is correct    
        var parameter = System.Linq.Expressions.Expression.Parameter(typeof (Array));
        return System.Linq.Expressions.Expression.Lambda<Action>(System.Linq.Expressions.Expression.Call(method, parameter));
    }

在这种情况下,我遇到异常{静态方法需要空实例,非静态方法需要非空实例.\ r \ n参数名称:方​​法"}.我正在为此而努力,但不确定是否这是我应该走的路.我整天都在努力,所以我希望有人能够帮助我加快学习速度.

In this case I am getting the exception {"Static method requires null instance, non-static method requires non-null instance.\r\nParameter name: method"}. I am working on that, but not sure if this is the road I should be going down. I have been working on this all day, so I was hoping someone might be able to help me speed up my learning.

推荐答案

您的第二个实例将在创建指向您指定方法的指针时起作用,但是要解决您的静态问题,您只需以以下两种方式之一修改以下行.首先,您可以通过声明要静态化的方法并修改以下代码行来完成静态引用:

Your second instance will work in creating a pointer to your specified method, but to solve your static issue you just need to modify the following line in one of 2 ways. First you can complete the static reference by declaring the method you seek to be static and modifying this line of code:

System.Linq.Expressions.Expression.Call(method, parameter);

您将必须为call方法提供一个null参数,因为如果您要搜索静态方法,则编译器将确切知道您想要的方法签名,因为它只会存在1.代码行为更新为:

You would have to provide a null parameter for the call method because if you are searching for a static method, then the compiler will know exactly what method signature you desire, because there will only exist 1. The line of code would be updated to:

System.Linq.Expressions.Expression.Call(null, method, parameter); 

第二种方法是定义与该方法相关的类或实例",以便编译器知道针对该类寻找方法签名.您将必须像这样修改您的代码:

The second approach is to define the class or "instance" that correlates to the method so that the compiler knows what class to search against for the method signature. You would have to modify your code like this:

var myInstance = Expression.Parameter(typeof(MyClass), "inst");
System.Linq.Expressions.Expression.Call(myInstance, method, parameter)

我建议查看致电,以便您确切地知道如何创建指针.

I recommend looking at the documentation for Call so that you know exactly how the pointer is being created.

这篇关于如何从字符串为Lambda表达式动态创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆