积极答复者
请问在C#2.0的程序里可以调用C#4.0开发的dll吗?

问题
-
因为主程序是2。0开发的,想要调用一个C#4。0开发的dll,能做到吗?怎么做呢?
- 已移动 Sheng Jiang 蒋晟Moderator 2011年8月14日 21:23 CLR (发件人:Visual C#)
答案
-
用require吧<configuration>...<startup><requiredRuntime version="4.0.30319" safemode="true"/></startup>...</configuration>
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP- 已标记为答案 mldark 2010年4月16日 11:00
全部回复
-
你的2.0程序要在CLR 4.0上运行,也就是在config里面加
<configuration><startup><supportRuntime version="4.0.20506"/></startup></configuration>
这样才可以运行依赖于CLR4.0的代码。
另外,.Net 4.0中去除了一些功能,比如CAS安全设置。显式指定CLR 2.0 CAS安全设置的程序集需要去掉CAS安全设置之后重新编译才可以被CLR 4.0加载。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP -
非常感谢您的回复。
我在主程序 控制台exe(.net2.0)的配置文件中加入:
<startup>
<supportRuntime version="4.0.30319"/>
</startup>程序中反射调用..net4.0的一个dll(它没有关联其他更多的dll)。
Assembly.LoadFile报错:
ex = {"生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。 (异常来自 HRESULT:0x8013101B)"}
改用Assembly.LoadFrom报错
ex = {"未能加载文件或程序集“file:///F:\\My Documents\\Visual Studio 2010\\Projects\\TestB\\bin\\Debug\\testb.dll”或它的某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。"}
至于直接引用reference,会出提示框,然后引用集有个小叹号。
var t = new CB();
编译报错:
Error 3 The type or namespace name 'CB' could not be found (are you missing a using directive or an assembly reference?)
至于您说的,CAS安全设置,我不知道在哪里去掉。
再次感谢您的帮助,期待您的回复!
我的技术博客:http://blog.csdn.net/begtostudy- 已编辑 白途思-Begtostudy 2010年4月16日 1:24 完善
-
用require吧<configuration>...<startup><requiredRuntime version="4.0.30319" safemode="true"/></startup>...</configuration>
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP- 已标记为答案 mldark 2010年4月16日 11:00
-
-
感谢您的提醒,我看了这方面的介绍。
但据我的理解In-Process Side By Side是C#4兼容低版本的程序,而低版本的.net是不具有这个能力,
是不是说就不能实现我想的低版本dll加载高版本的dll
我尝试使用 *.dll.config 和 requiredRuntime,但是似乎没有效果。
采用2.0的dll反射调用4.0的dll
LoadFrom报错:
Could not load file or assembly 'file:///E:\MyDocuments\Visual Studio 2010\Projects\NX6_Open_CS_Wizard1\TestC\bin\Debug\TestC.dll' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
Assembly.LoadFile报错:
ex = {"This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. (Exception from HRESULT: 0x8013101B)"}
我想请教一下,LoadFrom有很多参数,可不可以设置一下调用4.0的dll?
再次表示感谢!
我的技术博客:http://blog.csdn.net/begtostudy -
非常感谢您的回复。
我查了一下宿主程序的机制,似乎是非.net程序开发的exe,通过一个nxclrldr.dll(C开发的),只有一个函数,就调用ManagerLoader.dll(.net2.0)。难道这个nxclrldr.dll里实现的调用就是用到您所说的CLR HostAPI?
而ManagerLoader.dll因为是.net开发的,我用reflector看到他的代码,是.net没错。我提取了它的代码,在.net4下重新编译,但是它不能被nxclrldr.dll加载。
我想问一下CLR HostAPI实现的调用和COM调用有什么区别吗?
我的技术博客:http://blog.csdn.net/begtostudy -
msdn.microsoft.com/en-us/library/9x0wh2z3(VS.90).aspx
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP -
也不行,导入出错。
Microsoft (R) .NET Framework Type Library to Assembly Converter 3.5.30729.1
Copyright (C) Microsoft Corporation. All rights reserved.
TlbImp : error TI0000 : The input file 'E:\MyDocuments\Visual Studio 2010\Projec
ts\ex_vs10_被低版本调用\NX6_Open_CS_Wizard1\bin\Debug\ug.dll' is not a valid typ
e library.
我的技术博客:http://blog.csdn.net/begtostudy -
嗯,看起来不用这么麻烦
你用一个文本编辑器打开csproj文件,然后编辑引用的属性,加上 <SpecificVersion>True</SpecificVersion>就可以了。
假定你的.Net 4类库名是ClassLibrary1的话,这个引用看起来是这个样子:
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">
<Project>{A6EB8D5E-5C2F-44D6-AF2E-C00D74025329}</Project>
<Name>ClassLibrary1</Name>
<SpecificVersion>True</SpecificVersion>
</ProjectReference>
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP -
这个方法是需要设置requiredRuntime为CLR4.0的。
理论上应该可以用COM做中转的,不过IDE不让你加而已。要不用像IE和Office那样用非托管COM类型库作为中转?这样需要你自己写idl之后用midl生成tlb。
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP