13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

196 CHAPTER 7 Conclud<strong>in</strong>g <strong>C#</strong> 2: the f<strong>in</strong>al featuresMost of list<strong>in</strong>g 7.7 is just sett<strong>in</strong>g up the situation—the three l<strong>in</strong>es with<strong>in</strong> Ma<strong>in</strong> are the<strong>in</strong>terest<strong>in</strong>g ones. The first l<strong>in</strong>e pr<strong>in</strong>ts “Chapter7.Configuration” as the compilerresolves Configuration to that type before mov<strong>in</strong>g out to the namespace root. Thesecond l<strong>in</strong>e <strong>in</strong>dicates that the type has to be <strong>in</strong> the global namespace, and so simplypr<strong>in</strong>ts “Configuration.” I <strong>in</strong>cluded the third l<strong>in</strong>e to demonstrate that us<strong>in</strong>g the globalalias you can still refer to types with<strong>in</strong> namespaces, but you have to specify the fullyqualified name.At this po<strong>in</strong>t we can get to any uniquely named type, us<strong>in</strong>g the global namespacealias if necessary—and <strong>in</strong>deed if you ever write a code generator where the codedoesn’t need to be readable, you may wish to use this feature liberally to make surethat you always refer to the correct type whatever other types are actually present bythe time the code is compiled. What do we do if the type’s name isn’t unique evenwhen we <strong>in</strong>clude its namespace? The plot thickens…7.4.3 Extern aliasesAt the start of this section, I referred to human names as examples of namespaces andcontexts. I specifically said that you’re unlikely to know more than one person calledJon Skeet. However, I know that there is more than one person with my name, and it’snot beyond the realm of possibility to suppose that you might know two or more of us.In this case, <strong>in</strong> order to specify which one you mean you have to provide some more<strong>in</strong>formation beyond just the full name—the reason you know the particular person,or the country he lives <strong>in</strong>, or someth<strong>in</strong>g similarly dist<strong>in</strong>ctive.<strong>C#</strong> 2 lets you specify that extra <strong>in</strong>formation <strong>in</strong> the form of an extern alias—a namethat exists not only <strong>in</strong> your source code, but also <strong>in</strong> the parameters you pass to the compiler.For the Microsoft <strong>C#</strong> compiler, this means specify<strong>in</strong>g the assembly that conta<strong>in</strong>sthe types <strong>in</strong> question. Let’s suppose that two assemblies First.dll and Second.dllboth conta<strong>in</strong>ed a type called Demo.Example. We couldn’t just use the fully qualifiedname to dist<strong>in</strong>guish them, as they’ve both got the same fully qualified name. Instead, wecan use extern aliases to specify which we mean. List<strong>in</strong>g 7.8 shows an example of the <strong>C#</strong>code <strong>in</strong>volved, along with the command l<strong>in</strong>e needed to compile it.List<strong>in</strong>g 7.8Work<strong>in</strong>g with different types of the same type <strong>in</strong> different assemblies// Compile with// csc Test.cs /r:FirstAlias=First.dll /r:SecondAlias=Second.dllextern alias FirstAlias;extern alias SecondAlias;us<strong>in</strong>g System;Specifies twoextern aliasesus<strong>in</strong>g FD = FirstAlias::Demo;Refers to extern aliasclass TestB with namespace alias{static void Ma<strong>in</strong>(){Console.WriteL<strong>in</strong>e(typeof(FD.Example));CUsesnamespacealiasLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!