简体   繁体   English

使用反射来动态调用函数会产生错误:非静态方法需要目标

[英]Using reflection to dynamically call a function gives error: Non-static method requires a target

I am trying to use reflection to call a function dynamically and not quite sure how to make it work. 我正在尝试使用反射来动态调用函数,但不确定如何使它工作。

Here's my main function: 这是我的主要功能:

public partial class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        while (true)
        {
            List<string> Firms = new List<string>();

            Firms.Add("BHP");

            foreach (string Firm in Firms)
            {

                typeof(WorkerRole).GetMethod(string.Format("{0}GetProfiles", Firm), BindingFlags.Instance | BindingFlags.Public).Invoke(null, null);
            }

            break;
        }

        Thread.Sleep(Timeout.Infinite);

    }
}

And here is an example of one of the functions I am dynamically calling (they all have the same signatures): 这是我动态调用的函数之一的示例(它们都具有相同的签名):

public partial class WorkerRole : RoleEntryPoint
{

    public List<string> BHPGetProfiles()
    {
        // Do tasks specific to BHP
    }
}

The error that I'm getting in the line that starts with typeof above is: 我从上述typeof开头的行中遇到的错误是:

Additional information: Non-static method requires a target. 附加信息:非静态方法需要一个目标。

I don't want to have my GetProfiles methods as static, but I thought that adding BindingFlags.Instance should have resolved this? 我不想将我的GetProfiles方法设置为静态,但我认为添加BindingFlags.Instance应该可以解决此问题?

Thanks for your help! 谢谢你的帮助!

You are right that BindingFlags.Instance means that your method is not static. 您说对了, BindingFlags.Instance意味着您的方法不是静态的。 As a result, your call to GetMethod is not returning null . 因此,您对GetMethod的调用未返回null

Instead, the problem is that your Invoke call is supplying null instead of the object for this in the call. 相反,问题是,你Invoke呼叫提供null的,而不是为对象this通话。 It should be the the first parameter. 它应该是第一个参数。 For example, if you want the method to be called for the object on which Run() is executing, you should use: 例如,如果要为正在执行Run()的对象调用该方法,则应使用:

typeof(WorkerRole).GetMethod(string.Format("{0}GetProfiles", Firm), 
    BindingFlags.Instance | BindingFlags.Public).Invoke(this, null);

More details on Invoke is on this MSDN page . 有关Invoke更多详细信息, Invoke MSDN页面

暂无
暂无

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

相关问题 收到错误消息:System.Reflection.TargetException:非静态方法需要一个目标 - Receiving error message: System.Reflection.TargetException: Non-static method requires a target EF非静态方法需要目标 - EF Non-static method requires a target 非静态方法需要一个目标 - Non-static method requires a target System.Reflection.TargetException:&#39;非静态方法需要目标。 仅适用于VS 2017而不适用于VS 2013 - System.Reflection.TargetException: 'Non-static method requires a target.' only with VS 2017 not with VS 2013 System.Reflection.TargetException是什么:非静态方法需要一个目标。 意思? - What does System.Reflection.TargetException: Non-static method requires a target. mean? 用户代码未处理System.Reflection.TargetException-非静态方法需要目标吗? - System.Reflection.TargetException was unhandled by user code - Non-static method requires a target? 在Reflection中调用非静态方法 - Call Non-Static method in Reflection 如何使用MVC3 WebApi和AngularJs更新记录。 收到此错误:非静态方法需要一个目标。 - How to update a record using MVC3 WebApi and AngularJs . Getting this error : Non-static method requires a target.< 邮递员为什么返回“非静态方法需要目标”。 - Why the postman is returning a “Non-static method requires a target.” 实体框架 dbset 附加非静态方法需要一个目标 - Entity Framework dbset attach non-static method requires a target
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM