委托给一个实例方法不能有null'this' [英] Delegate to an instance method cannot have null 'this'

查看:1544
本文介绍了委托给一个实例方法不能有null'this'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C#.NET 2.0应用程序,其中在运行时,根据环境加载两个DLL之一。两个DLL都包含相同的功能,但它们没有链接到相同的地址偏移。我的问题是在我的应用程序代码中的函数代码。

  public class MyClass 
{
public delegate int MyFunctionDelegate(int _some,string _args);

public MyFunctionDelegate MyFuncToCallFrmApp;

public MyClass():base()
{
this.MyFuncToCallFrmApp = new MyFunctionDelegate(this.MyFuncToCallFrmApp); //< - - 这里抛出异常。
}

public SomeFunction()
{
MyFuncToCallFrmApp(int _someOther,string _argsLocal);
}
}

当我的代码执行时,我得到一个的委托给一个实例方法不能为空这个'。我做错了什么?

解决方案

您需要分配一个有效的功能(由动态加载的DLL中的某些类托管)你的委托变量。如果函数是具有相同名称​​的类的静态方法,那么这是很简单的:

  public MyClass (){
this.MyFuncToCallFrmApp = ExternalClass.Function;
}

如果函数是具有相同名称的类的实例方法,只需创建一个实例和做同样的事情(还要注意,只要委托在范围内,它将阻止 ExternalClass 实例被垃圾回收 - 您可能想要存储实例作为一个成员变量,使其更清晰):

  public MyClass(){
this.MyFuncToCallFrmApp = new ExternalClass ()。功能;
}

如果动态加载的类有不同的名称,则需要确定在这个例子中,我使用一个布尔成员变量来决定是否使用默认程序集的类:

  public MyClass(){
if(this.defaultAssembly){
this.MyFuncToCallFrmApp = ExternalClass1.Function;
} else {
this.MyFuncToCallFrmApp = ExternalClass2.Function;
}
}


I am developing a C# .NET 2.0 application wherein at run-time one of two DLLs are loaded depending on the environment. Both DLLs contain the same functions, but they are not linked to the same address-offset. My question is regarding the function delegates in my application code.

public class MyClass
{
    public delegate int MyFunctionDelegate(int _some, string _args);

    public MyFunctionDelegate MyFuncToCallFrmApp;

    public MyClass() : base()
    {
        this.MyFuncToCallFrmApp = new MyFunctionDelegate(this.MyFuncToCallFrmApp); // <-- Exception thrown here.
    }

    public SomeFunction()
    {
        MyFuncToCallFrmApp(int _someOther, string _argsLocal);
    }
}

When my code executes I get an ArgumentException of "Delegate to an instance method cannot have null 'this'." What am I doing wrong?

解决方案

You need to assign a valid function (hosted by some class in the dynamically loaded dll) to your delegate variable. If the functions are static methods on classes with the same name, this is straightforward:

public MyClass() {
    this.MyFuncToCallFrmApp = ExternalClass.Function;
}

If the functions are instance methods of classes with the same name, just create an instance and do the same thing (also note that as long as the delegate is in scope, it will prevent the ExternalClass instance from being garbage-collected - you may want to store the instance as a member variable to make that clearer):

public MyClass() {
    this.MyFuncToCallFrmApp = new ExternalClass().Function;
}

If the dynamically-loaded classes have different names, you'll need to determine which one to call - in this example, I'm using a boolean member variable to decide whether or not to use a default assembly's class:

public MyClass() {
    if (this.defaultAssembly) {
        this.MyFuncToCallFrmApp = ExternalClass1.Function;
    } else {
        this.MyFuncToCallFrmApp = ExternalClass2.Function;
    }
}

这篇关于委托给一个实例方法不能有null'this'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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