Home   Cover Cover Cover Cover
 

Assemblies and Modules

Example "multi file assembly"

Assemblies combine modules and resource files and represent under .NET the (logical (as opposed to physical)) unit for deployment, encapsulation, versioning and security.
In the example we first generate a module priv.mod from PrivateType.cs.

PrivateType.cs
internal class PrivateType {
  static int total = 0;
  int count = 0;
  
  internal PrivateType () { count = total++; }
  
  internal string GetKind () { return "PRIVATE"; }
  internal int GetCount () { return count; }
}

>csc /target:module /out:priv.mod PrivateType.cs /nologo
Afterwards we generate a second module pub.mod from PublicType.cs, which uses the module priv.mod (priv.mod remains in a separate physical file!).

PublicType.cs
public class PublicType {
  PrivateType obj;
  
  public PublicType () { obj = new PrivateType(); }
  
  // This method must not be public,
  // because the type of its argument o is internal.
  internal void SetObj (PrivateType o) { obj = o; }

  public int GetNum () { return obj.GetCount(); }
  public override string ToString () { 
    return "This PUBLIC object has " + obj.GetKind() + "#" + GetNum(); 
  }
}

>csc /target:module /out:pub.mod /addmodule:priv.mod PublicType.cs /nologo
The main file of our application is MFAApp.cs. From this we generate the manifest module of the multi file assembly by adding the modules priv.mod and pub.mod.

MFAApp.cs
using System;

public class App {
  public static void Main () {
    PublicType obj1 = new PublicType();
    Console.WriteLine(obj1);
    
    obj1.SetObj(new PrivateType());
    Console.WriteLine(obj1);    
  }
}

>csc /addmodule:priv.mod,pub.mod MFAApp.cs /nologo

When we run the application, we see that PublicType object actually get access to PrivateType objects and their components.
>MFAApp.exe
This PUBLIC object has PRIVATE#0
This PUBLIC object has PRIVATE#1

Example "types from other assemblies"

Now we create another assembly OtherApp from OtherApp.cs which wants to use both the types from MFAApp.

OtherApp.cs
using System;

public class OtherApp {
  public static void Main () {
    PublicType pubObj = new PublicType();
    
    Console.WriteLine(pubObj);

    pubObj.setObj(new PrivateType());
    
    Console.WriteLine(pubObj);
  }
}

When generating the assembly we must indicate that we want to use MFAApp.
>csc /reference:MFAApp.exe OtherApp.cs /nologo
OtherApp.cs(9,21): error CS0122:
	'PrivateType' is inaccessible due to its protection level
This, of course, must not work in this way, because we determined that the type PrivateType is only visible from within the defining assembly.
Other applications can only create and use PublicType objects, while PrivateType remains unreachable for them.
In order to allow compilation and execution of OtherApp we have to remove the parts that use PrivateType.

OtherApp2.cs
using System;

public class OtherApp {
  public static void Main () {
    PublicType pubObj = new PublicType();
    
    Console.WriteLine(pubObj);
  }
}

>csc /reference:MFAApp.exe OtherApp2.cs /nologo

>dir Other* /b
OtherApp.cs
OtherApp2.cs
OtherApp2.exe

>OtherApp2.exe
This PUBLIC object has PRIVATE#0