visual-studio 如何创建一个dll文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2891258/
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
How to create a dll file
提问by Gopal
Using Visual Studio 2005
使用 Visual Studio 2005
I have list of class files, when i try to run the class files, it showing error as "a project with output type of class library cannot be started directly"
我有类文件列表,当我尝试运行类文件时,它显示错误为“无法直接启动具有类库输出类型的项目”
How to run the class file? How to create a dll file.
如何运行类文件?如何创建一个dll文件。
Am new to visual studio 2005
我是 Visual Studio 2005 的新手
Need Help?
需要帮忙?
回答by Hans Olsson
A Class Libraryis just that, a library of code, you need to create an application that references the library to try it out.
In the same solution, just add a new project as a Winforms Application and then in the winforms application project add a reference to the class library project.
AClass Library就是这样,一个代码库,您需要创建一个引用该库的应用程序来试用它。
在同一个解决方案中,只需添加一个新项目作为Winforms应用程序,然后在winforms应用程序项目中添加对类库项目的引用。
You should then be able to call the methods in the library from the application code.
然后,您应该能够从应用程序代码调用库中的方法。
回答by user330592
To create a DLL File, click on New project, then select Class Library.
要创建 DLL 文件,请单击“新建项目”,然后选择“类库”。
Enter your code into the class file that was automatically created for you and then click Build Solution from the Debug menu.
将您的代码输入到自动为您创建的类文件中,然后从“调试”菜单中单击“构建解决方案”。
Now, look in your directory: ../debug/release/YOURDLL.dll
现在,查看您的目录:../debug/release/YOURDLL.dll
There it is! :)
就在那里!:)
P.S. DLL files cannot be run just like normal applciation (exe) files. You'll need to create a separate project (probably a win forms app) and then add your dll file to that project as a "Reference", you can do this by going to the Solution explorer, right clicking your project Name and selecting Add Reference then browsing to whereever you saved your dll file.
PS DLL 文件不能像普通的应用程序 (exe) 文件一样运行。您需要创建一个单独的项目(可能是一个 win 表单应用程序),然后将您的 dll 文件作为“参考”添加到该项目中,您可以通过转到解决方案资源管理器,右键单击您的项目名称并选择添加来执行此操作参考然后浏览到您保存 dll 文件的任何位置。
Then, to be able to use this dll file, in your projects code, you call the methods inside the dll file. For example:
然后,为了能够使用这个 dll 文件,在您的项目代码中,您调用 dll 文件中的方法。例如:
If, in your DLL file you have a method like this:
如果,在您的 DLL 文件中,您有这样的方法:
public string somerandommethod()
{
string x = "something";
return x;
}
Then, in your Form1.cs file of your separate project, you would call the code from your dll file like this:
然后,在单独项目的 Form1.cs 文件中,您可以像这样从 dll 文件中调用代码:
button1_Click(object sender, EventArgs e)
{
MyDllFile dll = new MyDllFile();
MessageBox.Show(dll.somerandommethod());
}
I hope this has helped you
我希望这对你有帮助
回答by Darin Dimitrov
You cannot run projects of type class library. You need to define a startup project that is either a console application, windows application or a web application which would use the class library.
您不能运行类库类型的项目。您需要定义一个启动项目,该项目是控制台应用程序、Windows 应用程序或将使用类库的 Web 应用程序。
回答by Pete Kirkham
回答by Ram
You can not run a class file, either you can go to project properties ->Application - > Output type. Here you can specify the application type as console application so your code will run on command prompt. Also make sure that the project you are trying to run is set as startup project (you can do it by right click on project and select "Set as Startup project".
您不能运行类文件,也可以转到项目属性 -> 应用程序 -> 输出类型。您可以在此处将应用程序类型指定为控制台应用程序,以便您的代码将在命令提示符下运行。还要确保您尝试运行的项目设置为启动项目(您可以通过右键单击项目并选择“设置为启动项目”来完成此操作。
To create a DLL you need to select New Project -> Class library.
要创建 DLL,您需要选择 New Project -> Class library。

