Create .NET 2.0 AppDomain in .NET 4.0 process

2019-05-24 04:57发布

问题:

I need to dynamically create a .NET 2.0 compatible assembly from within my .NET 4.0 process. Currently it is achieved with this:

AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(...)
ModuleBuilder  mb = assemblyBuilder.DefineDynamicModule(...);

But unfortuntely all dll's produced are .NET 4.0 (inherited from my 4.0 process) which doesn't work with my other .NET 2.0 processes.

Any idea how 2 different CLR versioned AppDomains can co-exist in the same process?

回答1:

Have a look at this question: http://social.msdn.microsoft.com/Forums/en/clr/thread/1bfd7f40-fd57-4c9f-803f-aa4b19214af9.

Paul Zhou explains, that it is possible to host multiple CLRs in the same Windows process and provides the following links with more detailed information:

  • CLR Hosting APIs
  • Creating a host application for the .NET Common Language Runtime
  • Host CLR

However, the links look like this is not exactly a trivial task...