0

I have a question, mostly out of curiosity, but also for a pet project:

Given a simplistic C# solution, where I would end up with an A.dll and a B.dll after building:

Solution
  - Project A
    - Class A1
    - Class A2
  - Project B
    - Class B1
    - Class B2

What I'm curious about, is: Is there any way for DotNet/Visual Studio/MS Build/whatever to generate DLLs differently?

A step in the right direction, would be to generate multiple DLL's from a single project, say A.A1.dll with just the A1 class and A.A2.dll with just the A2 class in it?

An even more interesting case would be to generate Lib1.dll with A1 and B1 and Lib2.dll with A2 and B2.

I do realize that the complexity of what I'm asking in a real life scenario is considerably higher than the simplistic description given.

I'm just curious if this is theoretically possible (without thousands of hours of work). And, if so, what approaches might I attempt?

Christian Rygg
  • 671
  • 12
  • 26
  • Why do you want to do so? – ProgrammingLlama Jan 18 '19 at 00:55
  • You can certainly create an additional project file that includes the files A1 and B1 separately using links. – Nick Larsen Jan 18 '19 at 00:56
  • You could also make some projects "Shared Projects" – StayOnTarget Jan 18 '19 at 00:57
  • Hi Christian, Are you thinking of the same thing as this question? https://stackoverflow.com/questions/8077570/how-to-merge-multiple-assemblies-into-one – StayOnTarget Jan 18 '19 at 00:57
  • 4
    If you're trying to split and join classes in that manner, do so with the project structure. Otherwise, it sounds like an XY problem. You'll need to explain what's really going on in order to get meaningful help. – madreflection Jan 18 '19 at 01:03
  • 1
    It is relatively simple to construct your own build targets and I think what you asking (compile each file into separate DLL) is somewhat trivial when classes are independent... Now reaching to that "relatively simple" state requires being good at MSBuild which in turn means reading and understanding good chunk of available information on it (i.e. read couple books). Even that alone is probably too broad for SO (and unlikely done by now)… If your classes actually depend on each other it would be much more fun to compute dependencies automatically (and way outside of scope of an SO question) – Alexei Levenkov Jan 18 '19 at 03:29
  • 1
    You still need to explain “what I'm asking in a real life scenario”. As projects are the smallest units to organize C# source files and compiled to assemblies, what you described above breaks the integrity and does not reveal any real world value to me. – Lex Li Jan 18 '19 at 03:36
  • @AlexeiLevenkov Thank you, this was the kind of answer I was looking for: Not an in depth explanation of what I must do, but a pointer to where in the stack I might attack such a problem. Regarding your last sentence: If you were to do something like this, would you look at MS Build for that too, or Roslyn, or somewhere else? – Christian Rygg Jan 18 '19 at 11:59
  • Regarding several of the other comments: Real world value isn't really relevant to my question - it was more of a "Can it be done?" Creating custom project structures or massaging the dlls post build would be a way to go, but I was wondering if this could be done during build. Somehow set up rules for what classes go into what DLL's. Thanks for the input, though. – Christian Rygg Jan 18 '19 at 12:04

1 Answers1

0

You can use the C# compiler directly to make DLL and EXE files. The compiler is part of the .NET Framework installation so you don't need Visual Studio to be able to run it.

Visual Studio project files are really just a convenient way of organising source files, dependencies and compiler settings. Visual Studio is a nice GUI on top of these project files.

Using the following files:

C:.
│   build.bat
│   Program.cs
│
├───ProjectA
│       ClassA1.cs
│       ClassA2.cs
│
└───ProjectB
        ClassB1.cs
        ClassB2.cs

ClassA1.cs:

public class ClassA1
{
public static void SayHello()
{
System.Console.WriteLine("ClassA1 says Hello World!");
}
}

ClassA2.cs:

public class ClassA2
{
public static void SayHello()
{
System.Console.WriteLine("ClassA2 says Hello World!");
}
}

Program.cs:

class Program
{
static void Main()
{
System.Console.WriteLine("Program says Hello World!");

ClassA1.SayHello();
ClassA2.SayHello();
}
}

We build everything with the C# compiler, csc.exe, using the batch script build.bat:

REM Building my project using the C# compiler
REM NOTE: Add the path to the compiler to the path variable:
REM       set path=%path;%windir%\Microsoft.NET\Framework\v4.0.30319

csc /out:A.A1.dll /target:library ProjectA\ClassA1.cs
csc /out:A.A2.dll /target:library ProjectA\ClassA2.cs

csc /out:Program.exe /r:A.A1.dll,A.A2.dll  Program.cs

This produces two DLLs (A.A1.dll and A.A2.dll) and one program (Program.exe) that uses the DLLs as shown below:

C:.
│   A.A1.dll
│   A.A2.dll
│   build.bat
│   Program.cs
│   Program.exe
│
├───ProjectA
│       ClassA1.cs
│       ClassA2.cs
│
└───ProjectB
        ClassB1.cs
        ClassB2.cs

Running the program gives the following output:

C:\Temp\HelloWorld>Program.exe
Program says Hello World!
ClassA1 says Hello World!
ClassA2 says Hello World!

C:\Temp\HelloWorld>
Nils Lande
  • 860
  • 9
  • 14