c# - 使用 Windows.winmd 引用库

标签 c# dll visual-studio-2015

我想做什么

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

什么在起作用

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

什么不起作用

我完成了我的 dll 的编写,并想在我的桌面应用程序中使用它。因此,我在我的测试项目中引用了 Arduino.dllWindows.winmd,希望以相同的方式工作。遗憾的是,在运行应用程序时,在为 Arduino.dll 实例化类时出现“找不到 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?

最佳答案

经过更多的研究,在 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

关于c# - 使用 Windows.winmd 引用库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41649597/

相关文章:

c# - 如何计算任意一组数字以及这些数字的所有子集的总和?

c# - 在 Windows 7 上运行 LibVLC.NET 示例

c++ - 如何在 win32 GUI 应用程序中向用户显示快速更新的数据?

visual-studio - 为什么当 Visual Studio 打开时,智能感知和代码建议停止工作?

c# - 分配 IEnumerable(协方差)

c# - 使用 List<int> 作为参数模拟一个方法,并使用 Moq 返回 List<>

c# - 如何查看 ASP.Net MVC 中调用的每个 Controller 和操作方法?

c++ - 运行时检查失败 #2 - 变量 '' 周围的堆栈已损坏

dll - 在DLL中转发数据

c# - Visual Studio 2015 "Find All References"只搜索打开的文件