简体   繁体   English

从C#中的类创建dll

[英]Creating a dll from classes in c#

I have a few classes with the same namespace, and I want to put all of them in a dll file, and use it in other places. 我有几个具有相同名称空间的类,我想将它们全部放入一个dll文件中,并在其他地方使用它。

I read the guide on Microsoft's website: 我在Microsoft网站上阅读了该指南:
http://msdn.microsoft.com/en-us/library/3707x96z(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/3707x96z(v=vs.80).aspx

They suggested this: 他们建议:

To build the file [myDllName].DLL, compile the two files [myClass1].cs and [myClass2].cs using the following command line: 要生成文件[myDllName] .DLL,请使用以下命令行编译两个文件[myClass1] .cs和[myClass2] .cs:

csc /target:library /out:[myDllName].DLL [myClass1].cs [myClass2].cs csc / target:library /out:[myDllName].DLL [myClass1] .cs [myClass2] .cs

I wrote it in a cmd window and got an error that there is no such command as csc. 我在cmd窗口中编写了该命令,但出现错误,提示没有csc这样的命令。
Plus, how it will find my files without a path? 另外,它将如何找到没有路径的文件?

So what is the right way of doing it? 那么正确的做法是什么? Where should I write it? 我应该在哪里写?

I saw some posts here with Library classes.. but not sure how it can help me. 我在这里看到了一些关于图书馆课程的帖子..但不确定如何帮助我。
My classes are in a different solution, and I just want to use it as an external dll, and not within the same solution. 我的班级在不同的解决方案中,我只想将其用作外部dll,而不是在同一解决方案中使用。
In this case, Microsoft's guide seems perfect, if it will work. 在这种情况下,如果可以的话,Microsoft指南似乎很完美。

If you use the Visual Studio Command Prompt window, all the necessary environment variables are set for you. 如果您使用Visual Studio命令提示符窗口,则将为您设置所有必需的环境变量。 In Windows 7, you can access that window from the Start menu by opening the Microsoft Visual Studio Version\\Visual Studio Tools folder. 在Windows 7中,可以通过打开Microsoft Visual Studio Version \\ Visual Studio Tools文件夹从“开始”菜单访问该窗口。 In Windows 8, the Visual Studio Command Prompt is called the Developer Command Prompt for VS2012, and you can find it by searching from the Start screen. 在Windows 8中,Visual Studio命令提示符称为VS2012开发人员命令提示符,您可以通过在“开始”屏幕中搜索来找到它。

source 资源

Why don't you just create a separate 'Class Library' project in your solution in Visual Studio? 为什么不在Visual Studio的解决方案中仅创建一个单独的“类库”项目?

You can than easily add it as a project reference to all other projects that need it. 然后,您可以轻松地将它添加为对所有其他需要它的项目的项目引用。

You can create Solution Folders in VS to logically group projects. 您可以在VS中创建解决方案文件夹,以对项目进行逻辑分组。

您可以从命令提示符运行vsvars批处理文件,它将为您设置路径,因为cmd.exe实例位于此处:

>"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat"

Make sure csc.exe is in your PATH environment variable. 确保csc.exe在您的PATH环境变量中。

You should have a vsvars32.bat that was installed with Visual Studio. 您应该具有随Visual Studio安装的vsvars32.bat。 You can run that to automatically add it to the path, or dig down in regedit to 您可以运行该命令以将其自动添加到路径,或在regedit中进行挖掘以

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP

Look through the subtrees in there for your framework version, and for an InstallPath key. 浏览其中的子树以获取您的框架版本和InstallPath密钥。 That should tell you what directory to look in. 那应该告诉您要查找的目录。

You can then use that, and: 然后,您可以使用它,并且:

WHERE /r "the_directory_you_found" csc.exe

to find the path. 找到路径。 Add that to your system environment variables, and you should be able to find it. 将其添加到系统环境变量中,您应该能够找到它。

As for the path of the class files, either run the command from the directory they're in, or specify the path when specifying the class files. 至于类文件的路径,请从它们所在的目录运行命令,或者在指定类文件时指定路径。

If you have Visual Studio installed, you could try creating a 'Class Library' project; 如果安装了Visual Studio,则可以尝试创建“类库”项目; Or you can run csc.exe by Launching "Visual Studio Command Prompt". 或者,您可以通过启动“ Visual Studio命令提示符”来运行csc.exe。

If you don't have Visual Studio installed but have .net framework installed. 如果您没有安装Visual Studio但已安装.net框架。 You can run csc.exe from the framework installation directory. 您可以从框架安装目录运行csc.exe。

Installation directory path for .net Framework 4.0 : .net Framework 4.0的安装目录路径:

C:\\WINDOWS\\Microsoft.NET\\Framework\\v4.0.30319 C:\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319

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

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