.NET 4 中的混合模式程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3179028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mixed mode assembly in .NET 4
提问by Alex Essilfie
I wrote a class library in .NET 2.0 for database access some 2 years ago and have been using it all along on .NET 2.0, 3.0 and 3.5.
大约 2 年前,我在 .NET 2.0 中编写了一个用于数据库访问的类库,并且一直在 .NET 2.0、3.0 和 3.5 上使用它。
In the current project I'm working on (which is a .NET 4.0 application), tried using the old faithful class library and I got the following exception:
在我正在处理的当前项目(这是一个 .NET 4.0 应用程序)中,尝试使用旧的忠实类库,但出现以下异常:
System.InvalidOperationException was unhandled
Message=An error occurred creating the form. See Exception.InnerException for details.
The error is: Mixed mode assembly is built against version 'v2.0.50727' of the runtime
and cannot be loaded in the 4.0 runtime without additional configuration information.
Source=SchoolManager
StackTrace:
at SchoolManager.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at SchoolManager.My.MyProject.MyForms.get_frmGeneric()
at SchoolManager.My.MyApplication.OnCreateMainForm() in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at SchoolManager.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.IO.FileLoadException
Message=Mixed mode assembly is built against version 'v2.0.50727' of
the runtime and cannot be loaded in the 4.0 runtime without additional
configuration information.
Source=Dinofage.Data.XpressData
StackTrace:
at Dinofage.Data.XpressData.ExecuteSelectInternal(String selectCommand)
at Dinofage.Data.XpressData.ExecuteSelect(String selectCommand)
at SchoolManager.Academics.GetNewAdmissionCode(String academicYear) in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\Modules\Academics.vb:line 89
at SchoolManager.StudentDetail..ctor() in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\UserControls\StudentDetail.vb:line 20
at SchoolManager.frmGeneric.InitializeComponent() in D:\Alex\Documents\Visual Studio 2008\Projects\School Manager\SchoolManager\frmGeneric.Designer.vb:line 25
at SchoolManager.frmGeneric..ctor()
InnerException:
What could be wrong and how do I fix it?
可能有什么问题,我该如何解决?
回答by Lasse V. Karlsen
The best would probably be to recompile your class library for .NET 4.0 in Visual Studio 2010 (ie. opening up the project, converting it, and changing the target framework.)
最好的方法可能是在 Visual Studio 2010 中为 .NET 4.0 重新编译您的类库(即打开项目、转换它并更改目标框架。)
If you can't, or won't, do that, then you can try adding the following to your app.config file for your .NET 4.0 application:
如果您不能或不会这样做,那么您可以尝试将以下内容添加到您的 .NET 4.0 应用程序的 app.config 文件中:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
ie.
IE。
<?xml version ="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
回答by Hans Passant
Since your assembly is mixed-mode, it potentially can call managed code from the unmanaged machine code in the assembly. With the new in-process side-by-side CLR version support in .NET 4.0, the runtime doesn't know which CLR version needs to be provided when that happens. You have to tell it that with an app.exe.config file that should look like this:
由于您的程序集是混合模式,它可能会从程序集中的非托管机器代码调用托管代码。借助 .NET 4.0 中新的进程内并行 CLR 版本支持,运行时不知道发生这种情况时需要提供哪个 CLR 版本。你必须告诉它一个 app.exe.config 文件应该是这样的:
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
</configuration>
回答by Ribeiro
Another way: In VB 2010 Express you can open your project and go to the tab Compile and then Advanced Compile Options.... Select .NET Framework 2.0from the drop-down list named Target framework (all configurations).
另一种方式:在 VB 2010 Express 中,您可以打开您的项目并转到选项卡编译,然后Advanced Compile Options.... .NET Framework 2.0从名为 的下拉列表中选择Target framework (all configurations)。
回答by Kishor Pawar
If you are not getting through even by specifying useLegacyV2RuntimeActivationPolicy="true", you may need to install a setup that is compatible with your OS and .NET version. You can find the same at http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki.
如果您甚至无法通过指定 useLegacyV2RuntimeActivationPolicy="true" 来完成,您可能需要安装与您的操作系统和 .NET 版本兼容的设置。您可以在http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 上找到相同的内容。
Before installing this setup, uninstall SQLite from installed programs. That will show errors in your code.
在安装此设置之前,请从已安装的程序中卸载 SQLite。这将显示您的代码中的错误。
If it is not working even after installing compatible setup, you have to remove the reference to your previous DLL file and add a new reference to this compatible DLL file.
如果即使在安装兼容设置后它也不起作用,您必须删除对先前 DLL 文件的引用,并添加对此兼容 DLL 文件的新引用。
When you add a reference to a compatible DLL file you just have to build your project and all your code errors should have gone.
当您添加对兼容 DLL 文件的引用时,您只需要构建您的项目,所有代码错误都应该消失了。
回答by Cenk
If anyone else still run into this issue, here is my diagnosis: You are using wrong SQLite package. There are many versions of SQLite under http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
如果其他人仍然遇到这个问题,这是我的诊断:您使用了错误的 SQLite 包。http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki下有很多版本的SQLite
For example, if you are targeting .net 4.5 with x86 platorm you should use this file:
例如,如果您的目标是使用 x86 平台的 .net 4.5,您应该使用以下文件:
under Precompiled Statically-Linked Binaries for 32-bit Windows (.NET Framework 4.5)
在 32 位 Windows (.NET Framework 4.5) 预编译静态链接二进制文件下
sqlite-netFx45-static-binary-bundle-Win32-2012-1.0.106.0.zip
sqlite-netFx45-static-binary-bundle-Win32-2012-1.0.106.0.zip
回答by REA_ANDREW
You could try opening the old faithful project (Class Library) inside of Visual Studio 2010 and allow it to do the conversion for you.
您可以尝试在 Visual Studio 2010 中打开旧的忠实项目(类库)并允许它为您进行转换。

