Creating Assemblies C# Help

Now that you know what assemblies are, it is time to build some. Of course, you have already built assemblies in previous chapters, because a .NET executable counts as an assembly. This section looks at special options for assemblies.

Creating Modules and Assemblies

All C# project types in Visual Studio create an assembly. Whether you choose a DLL or EXE project type, an assembly is always created. With the command-line C# compiler csc, it’s also possible to create modules. A module is a DLL without assembly attributes (so it’s not an assembly, but it can be added to assemblies at a later time). The command.

csc /target:module hello.cs

creates a module hello. netmodule. You can view this module using ildasm.

A module also has a manifest, but there is no . assembly entry inside the manifest (except for the external assemblies that are referenced) because a module has no assembly attributes. It’s not possible to configure versions or permissions with modules; that can be done only at the assembly scope. You can find references to assemblies in the manifest of the module. With the laddmodule option of csc, it’s possible to add modules to existing assemblies.

To compare modules to assemblies, create a simple class A and compile it by using the following command:

csc /target:module A.cs

The compiler generates the file A. net module, which doesn’t include assembly information (as you can setting ildasm to look at the manifest information). The manifest of the module shows the referenced assembly mscorlib and the .module entry.

Figure 17.4

Next, create an assembly B, which includes the module A.netmodule.It’s not necessary to have a source file to generate this assembly. The command to build the assembly is:

csc /target:library laddmodule:A.netmodule /out:B.dll

Looking at the assembly using i ldasm. you can find only a manifest. In the manifest, the assembly ms~rlib is referenced. Next, you see the assembly section with a hash algorithm and the version. The number of the algorithm defines the type of the algorithm that was used to create the hash code of the assembly. When creating an assembly programmatically it is possible to select the algorithm. Part of the manifest is a list of all modules belonging to the assembly. you see .module A .netmodule, which belongs to the assembly. Classes exported from modules are part of the assembly manifest; classes exported from the assembly itself,are not.

What’s the purpose of modules? Modules can be used for faster startup of assemblies because not all types are inside a single file. The modules are loaded only when needed. Another reason to use modules is if you want to create an assembly with more than one programming language. One module could be written using Visual Basic,another module could be written using C#, and these two modules could be included in a single assembly.

Figure 17-5

Assembly Attributes

When creating a Visual Studio project, the source file AssemblyInfo. cs is generated automatically. You can find this file below Properties in Solution Explorer. You can use the normal Source Code editor to configure the assembly attributes in this file. This is the file generated from the project template:

Capture

Capture

This file is used for configuration of the assembly manifest. The compiler reads the assembly attributes to inject the specific information into the manifest.

The-assembly: prefix with the attribute marks an assembly-level attribute. Assembly-level attributes are, in contrast to the other attributes, not attached to a specific language element. The arguments that can be used for the assembly attribute are classes of the namespaces System. Reflection, System .Runtime.Compilerservices!andsystem.Runtime.Interopservices.

The following table contains a list of assembly attributes defined within the System. Reflection namespace.

Capture

Capture

Here’s an example of how these attributes might be configured:

Capture

With Visual Studio 2008 you can configure these attributes with.the project properties, Application settings, and Assembly Information, as you can see.

Figure 17-6

Posted on October 31, 2015 in Assemblies

Share the Story

Back to Top