我正在使用.net4安装程序项目安装我的应用程序,这是用.net 4编写的,现在的问题是我在我的安装程序中使用.net2的拖曳程序集,所以当我运行安装程序时它会失败并显示此消息 “混合模式程序集是针对运行时版本'v2.0.50727'构建的,如果没有其他配置信息,则无法在4.0运行时加载。”现在问题可以通过app.config中的这一行来解决
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
问题是我无法在安装程序类中执行此操作 我该怎么办?
答案 0 :(得分:1)
经过深入搜索后我找到了解决方案 用户通过代码
使用LegacyV2RuntimeActivationPolicypublic 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
}