简体   繁体   English

在interop(C ++托管)项目中使用C#dll(使用Microsoft.bcl构建)

[英]Using C# dll (built with Microsoft.bcl) in interop(C++ managed) project

we've a C# DLL in .net4.0 but built using using Microsoft.bcl, Microsoft.bcl.async, Microsoft.bcl.build, Microsoft.net.http. 我们在.net4.0中有一个C#DLL,但是使用Microsoft.bcl,Microsoft.bcl.async,Microsoft.bcl.build,Microsoft.net.http构建。 These libs got from nuget. 这些库来自nuget。 We've reasons to not to move to .net4.5 but want to use async, await from these bcl libs. 我们有理由不转移到.net4.5但想要使用async,等待来自这些bcl库。 Everything works fine in C# projects, but we couldnt add this DLL in our C++ interop projects, we get this error: 在C#项目中一切正常,但我们无法在我们的C ++互操作项目中添加此DLL,我们收到此错误:

We get this error when we try to add this reference to project. 当我们尝试将此引用添加到项目时,我们收到此错误。

Even though clr interop project is also in .net4.0 and DLL we are adding is also in .net4.0 we end up getting this error. 即使clr interop项目也在.net4.0和我们添加的DLL也在.net4.0中我们最终得到这个错误。 Is there a way to resolve this? 有办法解决这个问题吗?

Error in text format:
---------------------------
Microsoft Visual Studio
---------------------------
Could not add a reference to:

C:\xxx\xxx\xx\xxxHelper.dll



For one of the following reasons:

    - Targets a higher version of the .NET Framework

    - Not a .NET assembly

    - Not a registered ActiveX control
---------------------------
OK   
---------------------------

code to reproduce this issue: https://dl.dropboxusercontent.com/u/1967630/BCL_Problem/oAuth2_SDK_consumer_DLL/BCL_Problem_projects.zipx 代码重现此问题: https//dl.dropboxusercontent.com/u/1967630/BCL_Problem/oAuth2_SDK_consumer_DLL/BCL_Problem_projects.zipx

I repro, sure looks like a bug. 我责备,肯定看起来像个臭虫。 Judging from the error message in your screenshot you are using VS2013. 从屏幕截图中的错误消息判断,您使用的是VS2013。 Very unhelpful message, it got toned down in VS2015 but still fails. 非常无益的消息,它在VS2015中降低了但仍然失败了。 Odd bug btw, seems like it treats the Microsoft.Threading.Task assembly reference that your C# project uses as a framework assembly. 奇怪的错误btw,似乎它对待你的C#项目用作框架程序集的Microsoft.Threading.Task程序集引用。 No prior SO questions about this issue that I know of, most programmers get this right without having to fight the machine. 我之前没有关于这个问题的SO问题,大多数程序员在没有打机的情况下做到了这一点。

You should consider doing it the way they do it to avoid this error. 您应该考虑按照这样做的方式来避免此错误。 This only goes wrong if you use the Browse button in the Add Reference dialog. 如果使用“添加引用”对话框中的“浏览”按钮,则只会出错。 But works just fine when you use a project reference, the way most programmers prefer to configure their solution. 但是在使用项目引用时工作得很好,这是大多数程序员喜欢配置解​​决方案的方式。 So use File > Add > Existing Project, select your C# project. 因此,使用File> Add> Existing Project,选择您的C#项目。 Then add a reference again but this time select the C# project from the Solution > Projects list instead of using the Browse button. 然后再次添加引用,但这次从Solution> Projects列表中选择C#项目,而不是使用Browse按钮。

If this is undesirable for some reason then you can work around this another way. 如果由于某种原因这是不受欢迎的,那么你可以用另一种方式解决。 Open your .vcxproj in a text editor, Notepad is fine. 在文本编辑器中打开你的.vcxproj,记事本很好。 Locate this element: 找到这个元素:

   <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

And modify the version number to v4.5 . 并将版本号修改为v4.5 Now add the reference like you did before with the Browse button, no complaints this time. 现在像使用Browse按钮一样添加引用,这次没有任何抱怨。 And go back to Notepad and change the version number back to v4.0 然后返回记事本并将版本号更改回v4.0

If nothing else helps, you can try to decompile your dll. 如果没有其他帮助,你可以尝试反编译你的DLL。 .Net assemblies are easy reversable. .Net组件易于拆卸。

Compiling it again may solve the issue (for example, your dll from nuget may appear to be 4.5, not 4.0). 再次编译可能会解决问题(例如,你的nuget中的dll可能看起来是4.5,而不是4.0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM