How to load a dll in an AppDomain with different NET version

683 Views Asked by At

I have some third party assembly which was build using Net 2.0. Although I can of course reference that assembly in net 4, running the app results in all kinds of strange errors.

So I though loading it in a separate application domain will fix the problem but it does not. I assume it is still executed using the Net 4 runtime. Is there any way to force execution of an application domain in a different net version ?

I use CreateInstanceFromAndUnwrap and than call the proxy.

Thanks for any help

Joe

2

There are 2 best solutions below

1
On

You can use the supportedRuntime configuration element to set the CLR version of an AppDomain. If you do not want to add this to your app.config, you can add a second .config file that will be used during the construction of the new AppDomain. Have a look at AppDomainSetup.ConfigurationFile for more info.

0
On
  1. Create AppDomain. And Create DomainManager:MarshalByRef object in new domain.
  2. DomainManager Load Assembly To created(new) domain.

AppDomainSetup ads = new AppDomainSetup(); AppDomain managerAD = AppDomain.CreateDomain("AD1", null, ads);

      Assembly asm = managerAD.Load(typeof(DomainManager).Assembly.FullName);
      string typeName = typeof(DomainManager).FullName;

      DomainManager manager = (DomainManager)managerAD.CreateInstanceAndUnwrap(asm.GetName().Name,

typeName); public class DomainManager : MarshalByRefObject {

    public void GetAppDomain(string assemblyFileName)
    {
      Assembly asm2 = Assembly.LoadFrom(assemblyFileName);
      Type neededType = asm2.GetType(<paste type>);
      object instance = Activator.CreateInstance(procType);
    }