C# 无法添加引用 .NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9619952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:12:22  来源:igfitidea点击:

cannot add a reference .NET

c#.netvisual-studio-2010

提问by Daniel

I have a DLL which I would like to add as a reference to my project, but everytime I try to do it a dialog pops up telling me:

我有一个 DLL,我想将其添加为对我的项目的引用,但是每次我尝试这样做时,都会弹出一个对话框告诉我:

The reference could not be added. Please make sure that the file is accesible, and that it is a valid assembly or COM component.

无法添加引用。请确保该文件可访问,并且它是有效的程序集或 COM 组件。

I have researched a little and found out that error is because the assembly is unmanaged by .NET and that I should use DLLImport Attribute, however, I have the same exact solution in another computer, and everythig just works fine.

我研究了一下,发现错误是因为程序集不受 .NET 管理,我应该使用 DLLImport 属性,但是,我在另一台计算机上有相同的解决方案,并且一切正常。

The difference is that the new computer I'm trying to add the reference is x64 and the old one is x86.

不同之处在于,我尝试添加引用的新计算机是 x64,而旧计算机是 x86。

I have both the x64 and x86 DLL's, and can't add any. Why is this happening?

我有 x64 和 x86 DLL,但无法添加。为什么会这样?

回答by Ben

Try changing your Platform Target from Any CPU to x86. (project properties -> Build -> Platform Target)

尝试将您的平台目标从任何 CPU 更改为 x86。(项目属性 -> 构建 -> 平台目标)

回答by Thorsten Dittmar

You can not add unmanaged DLLs as references in Visual Studio, regardless of the 32/64 "bittyness". And I doubt that it worked on your x86 machine.

无论 32/64“位数”如何,您都不能在 Visual Studio 中添加非托管 DLL 作为引用。而且我怀疑它是否适用于您的 x86 机器。

There's a difference between "normal" DLLs and COM-DLLs.

“普通”DLL 和 COM-DLL 之间存在差异。

You can add a reference to a COM-DLL after it was registered with the system (actually you reference the exposed COM object and a reference to the DLL is added automatically). This can be done on the "COM"-Tab of the "Add reference" dialog (here you have to make sure that your project is built as a x86 target in most cases).

在系统注册后,您可以添加对 COM-DLL 的引用(实际上您引用了公开的 COM 对象,并且会自动添加对 DLL 的引用)。这可以在“添加引用”对话框的“COM”选项卡上完成(在这里您必须确保您的项目在大多数情况下构建为 x86 目标)。

"normal" DLLs can - as I said - not be added at all. You can includethem in the solution (right click solution, select "Add existing file"), but they will not be referenced unless you declare something like

“普通”DLL 可以 - 正如我所说 - 根本不添加。您可以它们包含在解决方案中(右键单击解决方案,选择“添加现有文件”),但除非您声明类似的内容,否则它们不会被引用

[DllImport("...")]
public static extern void MyFunction();

I suspect that in your other solution, there's some kind of wrapper DLL, which you are actually referencing and which contains the DLL imports.

我怀疑在您的其他解决方案中,有某种包装 DLL,您实际上正在引用它并且其中包含 DLL 导入。

回答by Olivier Jacot-Descombes

Possibly the Type Library Importer (Tlbimp.exe)can help. It creates a wrapper .NET dll. The original COM DLL must still be there and must be registered! (Try to register it first, before trying it with TlbImp.)

可能类型库导入程序 (Tlbimp.exe)可以提供帮助。它创建了一个包装器 .NET dll。原始的 COM DLL 必须仍然存在并且必须被注册!(尝试先注册它,然后再尝试使用 TlbImp。)

If the 64-bit version does not work, set the platform target to x84in your project build properties.

如果 64 位版本不起作用,请x84在项目构建属性中将平台目标设置为。

回答by squelos

Some DLL's cant be added as a reference, but however, they can still be used by C# with the famous [DllImport( params go here...)]

某些 DLL 不能作为参考添加,但是,它们仍然可以被 C# 使用,并具有著名的 [DllImport( params go here...)]

You might also have to inspectthe dll in order to get the address of the functions you want to use . This can be achieved by using GetProcAddress

您可能还必须inspect使用 dll 才能获得要使用的函数的地址。这可以通过使用来实现GetProcAddress

回答by Uday0119

There are multipleansweres to your question:

您的问题有多种答案:

  1. you may face this problem because the assembly you are trying to add is targeted and compiled for a x86 processor architecture. Just try change the Target Platform from x64to x86and even if that doent work, try change it to AnyCPU. AnyCPU Platform target makes your application executable on both types of architecture because it is architecture-free.

  2. If the assembly happens to be a DLL, and it cannot be added as a reference, then it is nota COMas well as .NETassembly. It will be a native assembly like others (for example, shell32.dll, user32.dl etc). You have to use them via DllImportattribute, but you must first check the documentationof that dll to get the list of functions implemented in that dll.

  1. 您可能会遇到这个问题,因为您尝试添加的程序集是针对 x86 处理器体系结构进行编译和编译的。只需尝试将目标平台从x64更改为x86,即使不起作用,也尝试将其更改为AnyCPU。AnyCPU 平台目标使您的应用程序可以在两种类型的架构上执行,因为它是无架构的。

  2. 如果组件恰好是一个DLL,并且它不能被添加作为参考,那么它是不是一个COM以及.NET组件。它将是与其他程序集一样的本机程序集(例如,shell32.dll、user32.dl 等)。您必须通过DllImport属性使用它们,但您必须首先检查该dll的文档以获取在该 dll 中实现的函数列表。

回答by Brainstroming

after trying to analyse the issue, I discover the following

在尝试分析问题后,我发现以下内容

[1] sometimes you see the reference in the reference manager, but you will not find the dll files in the mentioned directory where the Reference manager is not able to actualize the references.

[1] 有时您会在引用管理器中看到引用,但在上述目录中找不到引用管理器无法实现引用的 dll 文件。

[2] check the permission of your dll file.

[2]检查你的dll文件的权限。