.NET 4中的混合模式汇编

Ale*_*fie 36 .net mixed-mode .net-4.0

我在大约2年前在.NET 2.0中编写了一个用于数据库访问的类库,并且一直在.NET 2.0,3.0和3.5上使用它.

在我正在进行的当前项目(这是一个.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: 

可能有什么问题,我该如何解决?

ang*_*son 72

最好的方法可能是在Visual Studio 2010中重新编译.NET 4.0的类库(即打开项目,转换它,以及更改目标框架.)

如果您不能或不会这样做,那么您可以尝试将以下内容添加到.NET 4.0应用程序的app.config文件中:

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
</startup>
Run Code Online (Sandbox Code Playgroud)

即.

<?xml version ="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
    </startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)


Han*_*ant 21

由于您的程序集是混合模式,因此它可能会从程序集中的非托管机器代码调用托管代码.通过.NET 4.0中新的进程内并行CLR版本支持,运行时不知道在发生这种情况时需要提供哪个CLR版本.你必须告诉它app.exe.config文件应该如下所示:

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • 在一个非常非常尴尬的地方,Excel.exe.config.任何人都可以搞乱它并杀死你的代码.在必要时考虑重建装配的时间很长. (4认同)