开发者

Usage Of AppDomain.CreateInstanceFromAndUnwrap()

I am attempting to load a dll into a seperate app domain using this code:

AppDomain domain = AppDomain.CreateDomain("HardwareAbstractionLayer");            
string pathToDll = @"DeviceManagement.dll";
Type t = 开发者_开发技巧typeof(DeviceManager);
DeviceManager myObject = (DeviceManager)domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName);

I get the error: "Constructor on type 'DeviceManagement.DeviceManager' not found."

It appears that the dll uses a singleton pattern and I'm not sure how to use the AppDomain function in that case. Here is the constructor code for the dll:

private DeviceManager() { }
private static readonly DeviceManager instance = new DeviceManager();
public static DeviceManager Instance { get { return instance; } }


What you'll need to do is create a wrapper object to create the instance for you.

public sealed class DeviceManagerWrapper : MarshalByRefObject {
  public DeviceManagerWrapper(){}
  public DeviceManager DeviceManager {
    get { return DeviceManager.Instance; }
  }
}

Now just create an instance of DeviceManagerWrapper and grab the DeviceManager singleton through the property.


You need to make a separate entry-point class with a public constructor that has a property that returns the singleton instance.

Both classes must inherit MarshalByRefObject.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜