使用Windows.winmd引用库

时间:2017-01-14 11:44:06

标签: c# dll visual-studio-2015

我想做什么

我正在尝试创建一个库(Arduino.dll),以便通过笔记本电脑与我的蓝牙设备进行交互(Windows 10)。此库旨在供桌面应用程序使用。 为实现这一目标,我必须同时引用Windows.winmdSystem.Runtime.WindowsRuntime

什么工作

我确实编写了库,并在同一解决方案中的另一个项目上的控制台应用程序上进行了测试。为了使它工作,我不得不在测试项目中引用项目和Windows.winmd。请注意,这个测试项目用于测试我的所有库,因此它被大量操作,并且我多年来一直在修改设置。

什么不起作用

我写完了我的dll,想在我的桌面应用程序中使用它。因此,我在我的测试项目中引用了Arduino.dllWindows.winmd,期望以相同的方式工作。可悲的是,在运行应用程序时,我找不到" dll not found"实例化Arduino.dll的类时:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in LuxAFX4.exe

Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.

奇怪,但好吧,我试图引用System.Runtime.WindowsRuntime中的Arduino.dll,但没有运气,我现在遇到了更奇怪的错误:

An unhandled exception of type 'System.BadImageFormatException' occurred in LuxAFX4.exe

Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

在我寻找解决方案期间,我明白我不应该尝试加载这个"参考库"如果没有找到,那一定是因为它没有安装在系统上。但是,为什么它会在我的测试项目中起作用?

我可能理解的另一件事是,如果我的图书馆没有公开引用的子库的成员,那么在引用我的库时就不需要它们了。我应该试着隐藏'所有参考?如果是这样,有没有办法捕获所有暴露的成员?资料来源:How to to avoid referencing a dll's referenced dll

附注:我使用C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dllC:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

可能相关:Could not load file or assembly Windows.winmdHow to reference Windows.winmd from a .NET Core library?

1 个答案:

答案 0 :(得分:0)

经过一些更多的研究,以及Hans Passant给出的提示,我终于明白我选择了错误的System.Runtime.WindowsRuntime.dll开始。将其更改为使用C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll代替使用DLL的项目和使用它的项目,解决了我的问题。

我在这里获得了新文件位置:C# “await” error when using WinRT from Desktop app