简体   繁体   English

如何将同一项目中的类编译为不同的DLL

[英]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: 关于您的代码,有两件事是正确的:

  1. Your classes are dependent on each other; 您的班级相互依赖; ie 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不会对自己的工作。
  2. Your classes are independent of each other; 您的班级彼此独立; ie Class1 has no relationship with Class2 . Class1Class2没有关系。 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.

相关问题 是否可以使用类库项目中的类子集编译dll? - Is it possible to compile a dll with subset of classes from the class library project? 如何在项目中编译dll并将其用作参考? - How to compile a dll in a project and use it like a reference? 在项目中使用相同DLL的不同版本 - Using different versions of the same DLL in a project Python for .NET:如何使用同一DLL的不同版本显式创建C#类的实例? - Python for .NET: How to explicitly create instances of C# classes using different versions of the same DLL? 为什么同样的dll暴露了C#和.net中的不同类 - Why same dll exposes different classes in C# and .net 将MVC项目编译为单个DLL? - Compile MVC project to a single DLL? 如何获得一个空项目以dll形式编译? - How do I get an empty project to compile as a dll? 一个项目可以使用同一个dll的2个不同版本吗? (Sybase.AdoNet 2和4) - Can a project use 2 different versions of the same dll? (Sybase.AdoNet 2 and 4) 从同一个项目编译几个dll并得到不同的结果 - compiling several dll's from same project and getting different result 使用 NSwag for C# 我们如何在同一个客户端项目中为不同的 API 创建多个客户端类 - With NSwag for C# how do we create multiple client classes for different API in the same client project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM