互操作程序集引用.NET框架的哪些功能? [英] Which features of .NET framework do interop assemblies reference?

查看:101
本文介绍了互操作程序集引用.NET框架的哪些功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含大约90个项目的Visual Studio解决方案.它们中的大多数都建立在DLL文件中,有些是用C ++编写的,有些是用C#编写的.这些项目通过COM相互通信.我们使用tlbexp生成某些C#项目(在C ++项目中引用的文件)的TLB文件.我们使用tlbimp生成C ++项目的互操作DLL.我没有完全深入探讨该主题,但是我认为,互操作文件仅定义C ++类的接口,以使它们可用于其他项目,对吗?

We have a Visual Studio solution with about 90 projects. Most of them are built to DLL files, some are written in C++, others in C#. The projects communicate with each other via COM. We use tlbexp to generate TLB files of some C# projects (the ones that are referenced in C++ projects). And we use tlbimp to generate interop DLLs of the C++ projects. I did not completely dig into the topic, but I think, the interop files just define the interfaces of the C++ classes, to make them usable from other projects is that right?

现在,问题如下:为了将整个解决方案升级到Visual Studio 2015,并使其针对.NET 4.6.1进行编译,我使用JetBrains的dotPeek检查了生成的程序集.我可以看到,所有C#项目都正确使用.NET 4.6.1,C ++ DLL本身是本机的,没有引用任何.NET.现在,令我惊讶的是dotPeek告诉我,互操作DLL(由C ++项目产生)正在引用.NET 4.0.

Now, the question is the following: In order to upgrade the entire solution to Visual Studio 2015 and let it compile against .NET 4.6.1, I inspected the resulting assemblies using dotPeek by JetBrains. I can see, that all the C# projects are correctly using .NET 4.6.1, the C++ DLLs themselves are native and do not reference any .NET. Now, what surprised me was the fact, that dotPeek told me, the interop DLLs (which resulted from the C++ projects), were referencing .NET 4.0.

经过一天的努力,使它们引用.NET 4.6.1,并且经过大量研究,我终于没有找到使interops引用.NET 4.6.1的任何方法.那有可能吗?我目前的猜测是,所有生成的互操作DLL都只是基本的.NET 4.0,只是因为它使用与.NET 4.6.1相同的CLR.

After a "wonderful" day of trying to make them reference .NET 4.6.1 and lots of research, I finally did not find any way to make the interops reference .NET 4.6.1. Is that even possible? My current guess is, that all interop DLLs generated like this reference just the basic .NET 4.0, just because it uses the same CLR as .NET 4.6.1.

是吗?应该可以在任何安装了.NET Framework 4.6.1的系统上执行程序集,不是吗?

Is that right? It should be possible to execute the assemblies on any system that has .NET Framework 4.6.1 installed, shouldn't it?

推荐答案

您从dotPeek获取错误信息.它没有告诉您如何确定目标.NET版本.它可以特定于编译器生成的.NET程序集.因为它会自动插入 [TargetFramework]属性进入程序集,它说明了构建项目时选择的.NET版本.

You are getting bad info from dotPeek. It doesn't tell you how it figured out what .NET version is targeted. It can be specific on a .NET assembly that's generated by a compiler. Because it automatically inserts a [TargetFramework] attribute into the assembly, it states what version of .NET you selected when you built the project.

但是互操作程序集未指定.NET版本,主要是因为它不是由编译器生成的.请注意,运行Tlbimp.exe时从未指定版本.而且不能. dotPeek可以弄清楚的是,它是由元数据格式指示的以.NET 4为目标的.没办法更具体.

But an interop assembly does not specify a .NET version, primarily because it isn't generated by a compiler. Note how you never specified a version when you ran Tlbimp.exe. And can't. All that dotPeek can figure out is that it targets .NET 4, cued by the metadata format. No way it can be more specific.

没关系,因为互操作库不使用.NET Framework功能.该库的唯一要点是您的程序可以使用COM组件功能.因此dotPeek报告的版本号无关紧要;任何.NET 4.x框架都可以使用该库.

And it does not matter, since the interop library does not use .NET Framework features. The only point of the library is that your program can use the COM component features. So the version number that dotPeek reports just doesn't matter; any .NET 4.x framework can use the library.

您没有真正的问题.

这篇关于互操作程序集引用.NET框架的哪些功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆