2014-02-28 122 views
0

我正在使用.net4安装程序项目来安装我的应用程序,它是用.net 4编写的。现在问题在于我在我的安装程序中使用了.net2的两个程序集,所以当我运行安装程序时,带有此消息 “混合模式程序集是针对运行时版本”v2.0.50727“构建的,无法在4.0运行时加载,无需其他配置信息。”现在的问题可以用这个线的app.config配置.net安装程序项目

<?xml version="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 
</configuration> 

问题是我不能在安装程序类 做到这一点我能做些什么来解决?

回答

1

终于深深的搜索后,我找到了解决办法 它是用户useLegacyV2RuntimeActivationPolicy通过代码

public static class RuntimePolicyHelper 
{ 
    public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; } 

    static RuntimePolicyHelper() 
    { 
     ICLRRuntimeInfo clrRuntimeInfo = 
      (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
       Guid.Empty, 
       typeof(ICLRRuntimeInfo).GUID); 
     try 
     { 
      clrRuntimeInfo.BindAsLegacyV2Runtime(); 
      LegacyV2RuntimeEnabledSuccessfully = true; 
     } 
     catch (COMException) 
     { 
      // This occurs with an HRESULT meaning 
      // "A different runtime was already bound to the legacy CLR version 2 activation policy." 
      LegacyV2RuntimeEnabledSuccessfully = false; 
     } 
    } 

    [ComImport] 
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
    [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")] 
    private interface ICLRRuntimeInfo 
    { 
     void xGetVersionString(); 
     void xGetRuntimeDirectory(); 
     void xIsLoaded(); 
     void xIsLoadable(); 
     void xLoadErrorString(); 
     void xLoadLibrary(); 
     void xGetProcAddress(); 
     void xGetInterface(); 
     void xSetDefaultStartupFlags(); 
     void xGetDefaultStartupFlags(); 

     [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
     void BindAsLegacyV2Runtime(); 
    } 
} 

然后我用它在我的代码是这样

if (RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully) 
    { 
    //my mixed mode dell call goes here 
    } 
+0

你应该包括信贷,信贷的是由于; http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/ – Tiny

相关问题