简体   繁体   English

.NET DLL问题

[英]A .NET DLL question

I created a C# file and wish to compile it into a DLL for future use. 我创建了一个C#文件,并希望将其编译为DLL供以后使用。 However, this .cs file depends on another DLL. 但是,此.cs文件取决于另一个DLL。 In my code inside my .cs file I am doing something like: 在我的.cs文件中的代码中,我正在执行以下操作:

using anotherlib.dll;

When I try to compile it into a DLL, the compiler tells me that it can't find that anotherlib.dll (missing directive or assembly reference). 当我尝试将其编译为DLL时,编译器告诉我找不到另一个lib.dll(缺少指令或程序集引用)。

What is the right way to go about it? 正确的做法是什么?

I am using .NET 2.0. 我正在使用.NET 2.0。

You need to add a reference to that particular DLL. 您需要添加对该特定DLL的引用。

If you are using Visual Studio try the following 如果您使用的是Visual Studio,请尝试以下操作

  1. Right click on your project in solution explorer 在解决方案资源管理器中右键单击您的项目
  2. Select Add Reference 选择添加参考
  3. Go to the Browse tab 转到浏览选项卡
  4. Navigate to the DLL location on disk and select OK 导航到磁盘上的DLL位置,然后选择“确定”。
  5. You might need to add a using statement to the desired namespace 您可能需要将using语句添加到所需的名称空间

If you have the source for the DLL, it's much better to use a project reference than a file reference. 如果您有DLL的源代码,那么使用项目引用比使用文件引用要好得多。 Simply add the project to the same solution, repeat steps 1-2 above and select Projects instead of Browse. 只需将项目添加到相同的解决方案,重复上述步骤1-2,然后选择“项目”而不是“浏览”。

If you are not using Visual Studio, then you need to pass the fully qualified path of the DLL to the compiler with the /r: flag. 如果不使用Visual Studio,则需要使用/ r:标志将DLL的标准路径传递给编译器。

You need to reference it using /r. 您需要使用/ r引用它。 If you are using the command line compiler. 如果使用命令行编译器。 Here's a link: http://msdn.microsoft.com/en-us/library/ms379563(VS.80).aspx 这是链接: http : //msdn.microsoft.com/zh-cn/library/ms379563(VS.80).aspx

If you are using Visual Studio, you simple add it as a reference in your project. 如果使用的是Visual Studio,则只需将其添加为项目中的引用即可。

A using statement is for importing a namespace. using语句用于导入名称空间。 You must also add an assembly reference to actually use the namespace. 您还必须添加一个程序集引用以实际使用名称空间。 If you are using csc.exe from the command line, you can specify an assembly reference with the command line argument /reference:filename.dll . 如果从命令行使用csc.exe ,则可以使用命令行参数/reference:filename.dll指定程序集引用。 If you are using Visual Studio, you can right click on your project and select "Add Reference...". 如果您使用的是Visual Studio,则可以右键单击您的项目,然后选择“添加引用...”。

You don't use the using statement in C# that way. 您不会以这种方式在C#中使用using语句。

Using, in C#, refers to the namespace. 在C#中,using是指名称空间。 You "include" the other DLL by "referencing" it in your project or compiler. 您可以通过在项目或编译器中“引用”另一个DLL来“包含”另一个DLL。 If you're using Visual Studio, add "anotherlib.dll" as a project reference, and then do: 如果您使用的是Visual Studio,请添加“ anotherlib.dll”作为项目引用,然后执行以下操作:

using TheNamespaceFromAnotherLibDLL;

You need to right click on the project in "Solution Explorer" and click on "Add Reference". 您需要右键单击“ Solution Explorer”中的项目,然后单击“ Add Reference”。

Browse for its location and add it as a reference. 浏览其位置并将其添加为参考。

This MSDN reference should provide more information. MSDN参考应提供更多信息。

Instead of saying "using" in your code, add it as an assembly reference. 与其在代码中说“使用”,不如将其添加为程序集引用。 In Visual Studio right-click on "References" and add the DLL. 在Visual Studio中,右键单击“参考”,然后添加DLL。 Then in your code have "using" for the namespaces of the stuff in the DLL. 然后在您的代码中将DLL中内容的名称空间 “使用”。

  1. Firstly, you need to add assembly reference in your project. 首先,您需要在项目中添加程序集引用。
  2. In Visual Studio right-click on "References" and add the DLL. 在Visual Studio中,右键单击“参考”,然后添加DLL。
  3. After that you may access that DLL in your code by using keyword 之后,您可以使用关键字在代码中访问该DLL。

And one more thing is that, You may put that DLL( You are accessing in your code ) in the bin folder of your project where your project DLL is generating. 还有一件事是,您可以将该DLL(您正在访问的代码中)放在生成项目DLL的项目的bin文件夹中。 Because suppose you are providing your DLL to others so you can easily give the bin folder. 因为假设您将DLL提供给其他人,所以您可以轻松地将bin文件夹命名。 So He/She friendly use your DLL. 因此,他/她友好地使用您的DLL。 And Never get error due to dependent DLL. 并且永远不会由于依赖DLL而导致错误。

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

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