问题描述
有人知道为什么在使用CLR4(.Net 4)时,编译在CLR2(.Net 3.5)下的C#创建的COM库不工作?在CLR4中缺少的是CLR2中的COM?
我们在app.config中使用适当的启动,使C#在CLR4 / .Net 4下运行:
< startup>
< supportedRuntime version =v4.0sku =。NETFramework,Version = v4.0/>
< / startup>
所有的C#都工作,直到它尝试创建我们的COM接口。它提供了以下异常:
当我们尝试从C ++创建COM接口时遇到同样的问题。
一旦我们安装CLR2(.Net 3.5),所有的COM东西开始工作。我们想知道发生了什么。
在.NET 4中编译程序集后,您可能需要使用regasm.exe来更新RuntimeVersion。如果重新编译不是一个选项,您可以将注册表中的RuntimeVersion从v2.0.50727更改为v4.0.30319。这对我有用。我在这里找到了类似的答案:
Does anyone know why a C# created COM library that was compiled under CLR2 (.Net 3.5) doesn't work when used with only CLR4(.Net 4)? What is missing in CLR4 that is in CLR2 for COM?
We are using the appropriate startup in the app.config to have the C# run under CLR4/.Net 4:
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
All the C# stuff works until it try's to create our COM interface. It provided the following exception:
We get the same issue when we try to create the COM interface from C++.
Once we install CLR2 (.Net 3.5) all the COM stuff starts working. We would like to know what's going on.
After compiling the assemblies in .NET 4, you'll probably need to use regasm.exe to update the RuntimeVersion. If recompiling is not an option, you could potentially change the RuntimeVersion in the registry from v2.0.50727 to v4.0.30319. This worked for me. I found a similar answer here: Using a .NET-2.0-targeted COM DLL in the GAC on a .NET-4-only system
这篇关于CLR2编译的C#COM不与.Net 4一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!