Use netmodules when compiling .NET assemblies?

I'm interested in examples of using netmodules in .NET. In particular, I was looking for better ways to break down solutions in .NET, but not many assemblies for deployment. Netmodules is a very interesting idea, but they display debugging interruptions and are not supported in Visual Studio (although they are designed for MSBuild) . I would rather rely on something native to .NET, which is why ILMerge, although not interesting in what I really want.

For some of my own projects, I am also starting to use FAKE , which allows me to take some interesting build steps, such as splitting test files . In other words, writing custom compilation steps is not a problem.

+4
source share
3 answers

The lack of answers makes me think that the other options noted in the comments and answers are usually considered better approaches than network modules. Thus, I assume that this means that no one uses or knows about the good use of network modules.

0
source

The netmodule itself is practically useless: their types and code cannot be loaded and executed by the runtime. You can only load types and execute code from assemblies, so it is likely that you will eventually combine several network modules into an assembly (multi-files). So let's look at the benefits of these.

Jeffrey Richter mentions three uses of multi-file collections in his CLR book through C # (p. 44), two of which are the result of using network modules:

  • "You can split your types between separate files, allowing you to incrementally download files [& hellip;]. Separating types into separate files also allows partial or separate packaging and deployment for the applications you buy and install."

    At the very least, the Microsoft CLI (.NET) implementation seems to load the assembly of several files gradually, and not completely from the very beginning. It seems that the module from the assembly is loaded from disk (or from the network?), When it really needs a type.

  • "You can create assemblies consisting of types implemented in different programming languages. [& Hellip;]"

    I'm not sure if this actually adds much value to the real-world scenario, (a) because Visual Studio does not support project links for network modules, and (b) since you can get the same benefits from assemblies.

    There is one noticeable difference between an approach with multiple assemblies and multiple files: one assembly by default cannot access other types of assemblies that have internal / Friend visibility (for example, an assembly). If you compile modules instead, and then link them to a single assembly of several files, a module compiled from C # could access the internal types of the module compiled using VB.NET (and vice versa).

    Below you will find a brief description of this.


    CsharpClass.cs:

     internal class CsharpClass { } 

    VbClass.vb:

     Friend Class VbClass : End Class 

    Program.cs:

     public static class Program { public static void Main() { var vbClass = new VbClass(); var csharpClass = new CsharpClass(); } } 

    Build a script for network modules:

     csc.exe /t:module /out:CsharpClass.netmodule CsharpClass.cs vbc.exe /t:module /out:VbClass.netmodule VbClass.vb csc.exe /t:exe /out:Program.exe /addmodule:CsharpClass.netmodule /addmodule:VbClass.netmodule Program.cs 

    This build will work and run without any errors.

    Note that there is nothing magical about the .netmodule file .netmodule ; this is only a convention, but the output file is a regular .NET DLL.

    Build script for assemblies:

     csc.exe /t:library /out:CsharpClass.dll CsharpClass.cs vbc.exe /t:library /out:VbClass.dll VbClass.vb csc.exe /t:exe /out:Program.exe /r:CsharpClass.dll /r:VbClass.dll Program.cs 

    This build will fail because:

     Program.cs(5,27): error CS0122: 'VbClass' is inaccessible due to its protection level Program.cs(5,23): error CS0143: The type 'VbClass' has no constructors defined Program.cs(6,31): error CS0122: 'CsharpClass' is inaccessible due to its protection level Program.cs(6,27): error CS0143: The type 'CsharpClass' has no constructors defined 
+4
source

Can you use Jeffrey Richter assembly deployment technique ? I have not tried it myself, but it looks promising.

+1
source

All Articles