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.

434 ❘ ChaPTer 18 Assemblies<br />

namespaces, assemblies, <strong>and</strong> Components<br />

You might be a little bit confused by the meanings of namespaces, types, assemblies, <strong>and</strong> components. How<br />

does a namespace fi t into the assembly concept The namespace is completely independent of an assembly.<br />

You can have different namespaces in a single assembly, but the same namespace can be spread across<br />

assemblies. The namespace is just an extension of the type name — it belongs to the name of the type.<br />

For example, the assemblies mscorlib <strong>and</strong> system contain the namespace System.Threading among many<br />

other namespaces. Although the assemblies contain the same namespaces, you will not find the same class names.<br />

Private <strong>and</strong> shared assemblies<br />

Assemblies can be shared or private. A private assembly is found either in the same directory as the<br />

application, or within one of its subdirectories. With a private assembly, it ’ s not necessary to think about<br />

naming confl icts with other classes or versioning problems. The assemblies that are referenced during<br />

the build process are copied to the application directory. Private assemblies are the usual way to build<br />

assemblies, especially when applications <strong>and</strong> components are built within the same company.<br />

Although it is still possible to have naming confl icts with private assemblies (multiple<br />

private assemblies may be part of the application <strong>and</strong> they could have confl icts, or a<br />

name in a private assembly might confl ict with a name in a shared assembly used by the<br />

application), naming confl icts are greatly reduced. If you fi nd you ’ ll be using multiple<br />

private assemblies or working with shared assemblies in other applications, it ’ s a good<br />

idea to utilize well - named namespaces <strong>and</strong> types to minimize naming confl icts.<br />

When using shared assemblies, you have to be aware of some rules. The assembly must be unique <strong>and</strong>,<br />

therefore, must also have a unique name, called a strong name. Part of the strong name is a m<strong>and</strong>atory<br />

version number. Shared assemblies will mostly be used when a vendor, different from that of the application,<br />

builds the component, or when a large application is split into subprojects. Also, some technologies, such as<br />

.<strong>NET</strong> Enterprise Services, require shared assemblies in specifi c scenarios.<br />

satellite assemblies<br />

A satellite assembly is an assembly that only contains resources. This is extremely useful for localization.<br />

Because an assembly has a culture associated, the resource manager looks for satellite assemblies containing<br />

the resources of a specifi c culture.<br />

You can read more about satellite assemblies in Chapter 22, “ Localization. ”<br />

Viewing assemblies<br />

You can view assemblies by using the comm<strong>and</strong> - line utility<br />

ildasm , the MSIL disassembler. You can open an assembly by<br />

starting ildasm from the comm<strong>and</strong> line with the assembly as<br />

an argument or by selecting the File ➪ Open menu.<br />

Figure 18 - 3 shows ildasm opening the example that you will<br />

build a little later in the chapter, SharedDemo.dll . ildasm<br />

shows the manifest <strong>and</strong> the SharedDemo type in the Wrox<br />

.ProCSharp.Assemblies namespace. When you open the<br />

manifest, you can see the version number <strong>and</strong> the assembly<br />

attributes, as well as the referenced assemblies <strong>and</strong> their versions.<br />

You can see the MSIL code by opening the methods of the class. figure 18-3<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!