[英]how to compile classes in the same project to different DLL
Hi all I have Class Library project with some classes. 大家好,我有一些课程的Class Library项目。
I would like to know how to compile each class to different DLL. 我想知道如何将每个类编译为不同的DLL。
I don't want to create separate project for each dll, 我不想为每个dll创建单独的项目,
I using this dll as plugins but these are very simple plugin and I don't want to create project for each plugins. 我使用此dll作为插件,但这些插件非常简单,我不想为每个插件创建项目。
I am going to put all these dll in the same directory and load them dynamically, 我将所有这些dll放在同一目录中并动态加载它们,
Thank you 谢谢
Create a seperate .cs file for each class. 为每个类创建一个单独的.cs文件。
Then create the DLLs just like it was suggested in Nygma7's answer by opening the Visual Studio Command Prompt, CD
to the location of the .cs files and create the DLLs at the prompt: csc /target:library /out:test1.dll test1.cs
etc.. 然后,按照Nygma7的答案中的建议创建DLL,方法是打开Visual Studio命令提示符CD
到.cs文件的位置,并在提示符下创建DLL: csc /target:library /out:test1.dll test1.cs
等。
为每个班级创建一个单独的项目。
I don't think this is a good way to proceed but, anyway. 我认为这不是进行下一步的好方法,但是无论如何。 You can exclude classes from your project and then compile. 您可以从项目中排除类,然后进行编译。
You can also use compiler directives as: 您还可以将编译器指令用作:
#define Class1
#if Class1
class 1
{
}
#endif
#undef Class2
#if Class2
class 2
{
}
#endif
And then has either Class1 or Class2 compiled within the output DLL. 然后在输出DLL中编译了Class1或Class2。 But, this will not be as automated as you want. 但是,这不会像您想要的那样自动化。 Can't figure other way. 想不通其他方法。
You need to stand back and think about what you're asking. 您需要退后一步,思考您的要求。 You have several classes that are all part of a single project, but you want that project to be built into several different pieces. 您有几个类都属于一个项目,但是您希望将该项目构建为几个不同的部分。 Why would you want to do that? 你为什么想这么做? One of two things is true about your code: 关于您的代码,有两件事是正确的:
Class1
depends on Class2
. 即Class1
取决于Class2
。 In this case, you do not want to build them into separate dlls because those dlls won't function on their own. 在这种情况下,你不希望将其打造成为独立的dll文件,因为这些dll不会对自己的工作。 Class1
has no relationship with Class2
. 即Class1
与Class2
没有关系。 In this case, why are in they in the same project? 在这种情况下,为什么它们在同一个项目中? These classes belong in their own project so that they can be built individually. 这些类属于自己的项目,因此可以单独构建。 After reading your update, I suspect you're in case #2. 阅读您的更新后,我怀疑您是第二种情况。 You should separate your classes into their own projects as long as they don't depend on each other. 只要您的类彼此不依赖,就应该将它们分成各自的项目。 You have said "I don't want to separate them", but you haven't said why . 您已经说过“我不想分开他们”,但是您没有说为什么 。 Do you have a reason why you don't want them separated? 您有理由不希望他们分开吗? You can still have them all part of the same solution, and you can still build them all with a single command. 您仍然可以将它们全部包含在同一解决方案中,并且仍然可以使用单个命令来构建它们。
Two Solutions: 1 You can separate the classes creating a new project in your solution, you will create a Class library, that will generate a dll. 两种解决方案:1您可以将类分开,在解决方案中创建一个新项目,您将创建一个类库,该类库将生成一个dll。 Than you must go to references and add the new reference to the project. 比您必须转到参考并将新参考添加到项目中。
You should think if this is a good solution, if you are going to use that class in others projects it's a good solution. 您应该考虑这是否是一个好的解决方案,如果您打算在其他项目中使用该类,那么它就是一个好的解决方案。
2 example of code... 2个代码示例...
using System;
namespace SimpleMaths
{
public class Operations
{
public static int add(int a, int b)
{
return a + b;
}
public static int substract(int a, int b)
{
return a - b;
}
public static int multiply(int a, int b)
{
return a * b;
}
}
}
Now open Start>>VisualStudio2005/2008 >> Visual Studio tools>>Visual Studio Command Prompt. 现在打开“开始” >>“ VisualStudio2005 / 2008” >>“ Visual Studio工具” >>“ Visual Studio命令提示符”。
Now in command Prompt Locate the Directory where you have saved the .cs file just created. 现在,在命令提示符下,找到保存了刚刚创建的.cs文件的目录。 Using CD command. 使用CD命令。
csc /target:library /out:File.dll test.cs csc / target:库/out:File.dll test.cs
this will generate the dll file that you want 这将生成您想要的dll文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.