问题描述
我已经是基于.NET 2运行时的应用程序。我想补充的.NET 4的支持,一点点,但不想(在短期内),将整个应用程序(这是非常大的)目标.NET 4。
I've an application that is based on .NET 2 runtime. I want to add a little bit of support for .NET 4 but don't want to (in the short term), convert the whole application (which is very large) to target .NET 4.
我试图创建一个应用程序config文件的明显的方法,有这样的:
I tried the 'obvious' approach of creating an application .config file, having this:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
但我遇到了一些问题,我注意到here.
我创建一个单独的应用程序域的概念。为了测试它,我创建了一个WinForm的项目目标。NET 2.然后,我创建一个类库针对.NET 4。在我的WinForm的项目,我增加了以下code:
I got the idea of creating a separate app domain. To test it, I created a WinForm project targeting .NET 2. I then created a class library targeting .NET 4. In my WinForm project, I added the following code:
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = "path to .NET 4 assembly";
setup.ConfigurationFile = System.Environment.CurrentDirectory +
"\\DotNet4AppDomain.exe.config";
// Set up the Evidence
Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
Evidence evidence = new Evidence(baseEvidence);
// Create the AppDomain
AppDomain dotNet4AppDomain = AppDomain.CreateDomain("DotNet4AppDomain", evidence, setup);
try
{
Assembly doNet4Assembly = dotNet4AppDomain.Load(
new AssemblyName("MyDotNet4Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=66f0dac1b575e793"));
MessageBox.Show(doNet4Assembly.FullName);
}
finally
{
AppDomain.Unload(dotNet4AppDomain);
}
我的DotNet4AppDomain.exe.config文件看起来是这样的:
My DotNet4AppDomain.exe.config file looks like this:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
不幸的是,这将引发的BadImageFormatException当执行dotNet4AppDomain.Load。难道我做错了什么在我的code,或者是什么,我要做的只是没有去上班?
Unfortunately, this throws the BadImageFormatException when dotNet4AppDomain.Load is executed. Am I doing something wrong in my code, or is what I'm trying to do just not going to work?
感谢您!
推荐答案
您瞄准2.0所以它是一个在内存中加载......他们你问它加载了一块4.0图像...它不能工作,你需要旋转的正确版本的新运行例如,如果你想这样做。
You target the 2.0 so it is the one loaded in memory... they you ask it to load a 4.0 image... it can't work you need to spin a new Runtime instance of the correct version if you want to do that.
要做到这一点的唯一方法可能是将举办第二CLR在你的过程就像在的巫成为可能与.net 4.0。
The only way to do that may be to Host a second CLR inside your process like explained in Is it possible to host the CLR in a C program? witch became possible with .Net 4.0.
这篇关于是否有可能加载一个新的应用领域针对不同的.NET运行时版本的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!