15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C r e a t i n g a s s e m b l i e s ❘ 435<br />

In addition to using ildasm , the .<strong>NET</strong> Refl ector is another great tool to use to analyze<br />

assemblies. The .<strong>NET</strong> Refl ector allows type <strong>and</strong> member search, <strong>and</strong> call <strong>and</strong> callee<br />

graphs, <strong>and</strong> decompiles IL code to <strong>C#</strong>, C++, or Visual Basic. You can download this<br />

tool from www.redgate.com/products/reflector .<br />

CreaTing assemblies<br />

Now that you know what assemblies are, it is time to build some. Of course, you have already built<br />

assemblies in previous chapters, because a .<strong>NET</strong> executable counts as an assembly. This section looks at<br />

special options for assemblies.<br />

Creating modules <strong>and</strong> assemblies<br />

All <strong>C#</strong> project types in Visual Studio create an assembly. Whether you choose a DLL or EXE project type,<br />

an assembly is always created. With the comm<strong>and</strong> - line <strong>C#</strong> compiler csc, it ’ s also possible to create modules.<br />

A module is a DLL without assembly attributes (so it ’ s not an assembly, but it can be added to assemblies at<br />

a later time). The comm<strong>and</strong> :<br />

csc /target:module hello.cs<br />

creates a module hello.netmodule . You can view this module using ildasm .<br />

A module also has a manifest, but there is no .assembly entry inside the manifest (except for the external<br />

assemblies that are referenced) because a module has no assembly attributes. It ’ s not possible to confi gure<br />

versions or permissions with modules; that can be done only at the assembly scope. You can fi nd references<br />

to assemblies in the manifest of the module. With the /addmodule option of csc , it ’ s possible to add<br />

modules to existing assemblies.<br />

To compare modules to assemblies, create a simple<br />

class A <strong>and</strong> compile it by using the following<br />

comm<strong>and</strong>:<br />

csc /target:module A.cs<br />

The compiler generates the fi le A.netmodule ,<br />

which doesn ’ t include assembly information (as<br />

you can see using ildasm to look at the manifest<br />

information). The manifest of the module shows the<br />

referenced assembly mscorlib <strong>and</strong> the .module entry<br />

in Figure 18 - 4.<br />

Next, create an assembly B, which includes the<br />

module A.netmodule . It ’ s not necessary to have a<br />

source fi le to generate this assembly. The comm<strong>and</strong> to<br />

build the assembly is:<br />

csc /target:library /addmodule:A.netmodule /out:B.dll<br />

figure 18-4<br />

Looking at the assembly using ildasm , you can fi nd only a manifest. In the manifest, the assembly<br />

mscorlib is referenced. Next, you see the assembly section with a hash algorithm <strong>and</strong> the version. The<br />

number of the algorithm defi nes the type of the algorithm that was used to create the hash code of<br />

the assembly. When creating an assembly programmatically it is possible to select the algorithm. Part of<br />

the manifest is a list of all modules belonging to the assembly. In Figure 18 - 5 you see .file A.netmodule ,<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!