简体   繁体   English

如何在.NET中将一个项目编译成多个dll

[英]How to compile one project into multiple dlls in .NET

I am trying to compile one .Net c# project into multiple dlls. 我试图将一个.Net c#项目编译成多个dll。 But here is the thing. 但事情就是这样。

This is my project structure. 这是我的项目结构。

Bin 
Dir1 
Dir2 
File1.cs 
File2.cs 
myproject.csproj

I want to compile this project into File1.dll and File2.dll. 我想将此项目编译为File1.dll和File2.dll。 Both File1.dll and File2.dll use code in various .cs files that are in Dir1 and Dir2. File1.dll和File2.dll都使用Dir1和Dir2中的各种.cs文件中的代码。 Some of these classes in the .cs files present in the sub directories are required by File1.cs and the others by File2.cs. 子目录中存在的.cs文件中的一些类是File1.cs所必需的,其他类是File2.cs。 There will be some that are used by both File1.cs and File2.cs. File1.cs和File2.cs都会使用一些。

I used the following: 我使用了以下内容:

csc /t:library /out:File1.dll /recurse:*.cs
csc /t:library /out:File2.dll /recurse:*.cs

But the resulting dlls were exact copies of each other with just different file names. 但是由此产生的dll是彼此的精确副本,只有不同的文件名。 Is there a way for me to compile them so that when I inspect each dll in the object browser, it has only those classes from the .cs files in the Dir1 and Dir2 that are referenced within File1.cs for File1.dll and similarly for File2.dll? 有没有办法让我编译它们,以便当我在对象浏览器中检查每个dll时,它只有Dir1和Dir2中.cs文件中的那些类,它们在File1.cs中为File1.dll引用,类似于File2.dll?

I want to do this preferably as a part of a post build step in Visual studio. 我想这样做最好是作为Visual Studio中的后期构建步骤的一部分。

Thanks for your time... 谢谢你的时间...

EDIT 编辑

Thanks for all your responses. 感谢您的所有回复。 There were many suggestions for splitting up the solution into many projects. 有很多建议将解决方案分成许多项目。 While that is one way to do it, I am not sure if I want the any dependencies created for the two dlls ie File1.dll and File2.dll. 虽然这是一种方法,但我不确定我是否想要为两个dll创建任何依赖项,即File1.dll和File2.dll。 Let me explain.. 让我解释..

The problem I see with creating multiple projects is that there will be some code that both File1.cs and File2.cs need. 我在创建多个项目时遇到的问题是File1.cs和File2.cs都需要一些代码。 I dont want them to be repeated in both projects. 我不希望它们在两个项目中重复出现。 Keeping the common code in a third project would mean a third dll will be created that is required for both File1.dll and File2.dll. 将公共代码保存在第三个项目中意味着将创建File1.dll和File2.dll所需的第三个dll。 I dont want this. 我不想要这个。 I want only File1.dll and File2.dll created which is why I am keeping them both in one project. 我只想创建File1.dll和File2.dll,这就是我将它们保存在一个项目中的原因。 Any suggestions on how to make this happen? 有关如何实现这一目标的任何建议?

Thanks again for your replies. 再次感谢您的回复。

cheers. 干杯。

You need to split your project up into multiple projects within the same solution if you want separate libraries for each component. 如果需要为每个组件使用单独的库,则需要将项目拆分为同一解决方案中的多个项目。

You can have the same file included in multiple projects without creating duplicated copies ( Add as link ) to avoid issues with duplicated code. 您可以在多个项目中包含相同的文件,而无需创建重复的副本( Add as link )以避免重复代码出现问题。

Create one project for each DLL you want to produce and add the file specific to each. 为要生成的每个DLL创建一个项目,并添加特定于每个项目的文件。 For example myproject1.csproj containing File1.cs (and other files as needed) and myproject2.csproj containing File2.cs . 例如myproject1.csproj包含File1.cs (和其他文件,如果需要)和myproject2.csproj包含File2.cs After that, create a third project containing any common files and reference that shared assembly from the first two. 之后,创建第三个项目,其中包含从前两个共享程序集的任何公共文件和引用。

Alternatively, if you want to include and exclude different pieces of code consider #if statements checking for preprocessor constants. 或者,如果要包含和排除不同的代码片段,请考虑#if语句检查预处理器常量。

You need multiple projects to do this. 您需要多个项目才能执行此操作。 There's nothing stopping you from referencing the same CS file from more than one project. 没有什么可以阻止您从多个项目引用相同的CS文件。

Also, if it makes sense to do so, both the projects can exist under the one solution, if you need the logical grouping. 此外,如果这样做有意义,如果您需要逻辑分组,则两个项目都可以存在于一个解决方案下。

You could use conditional compilation symbols ( #if DLL1 , #if DLL2 ). 您可以使用条件编译符号( #if DLL1#if DLL2 )。

Then, in your build step, call msbuild with the /p:DefineConstants option to set those symbols. 然后,在构建步骤中,使用/p:DefineConstants选项调用msbuild以设置这些符号。

MSBUILD /p:DefineConstants=DLL1 ...

basically compiling your project twice with different conditional compilation symbols set. 基本上用不同的条件编译符号集编译你的项目两次。

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

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