Robert
Robert

Reputation: 6226

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; } }

Upvotes: 1

Views: 6148

Answers (2)

JaredPar
JaredPar

Reputation: 755327

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.

Upvotes: 3

SLaks
SLaks

Reputation: 887967

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.

Upvotes: 0

Related Questions