Prev | Current Page 374 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"


C# 2 lets you specify that extra information in the form of an extern alias??”a name
that exists not only in your source code, but also in the parameters you pass to the compiler.
For the Microsoft C# compiler, this means specifying the assembly that contains
the types in question. Let??™s suppose that two assemblies First.dll and Second.dll
both contained a type called Demo.Example. We couldn??™t just use the fully qualified
name to distinguish them, as they??™ve both got the same fully qualified name. Instead, we
can use extern aliases to specify which we mean. Listing 7.8 shows an example of the C#
code involved, along with the command line needed to compile it.
// Compile with
// csc Test.cs /r:FirstAlias=First.dll /r:SecondAlias=Second.dll
extern alias FirstAlias;
extern alias SecondAlias;
using System;
using FD = FirstAlias::Demo;
class Test
{
static void Main()
{
Console.WriteLine(typeof(FD.Example));
Listing 7.8 Working with different types of the same type in different assemblies
Specifies two
extern aliases
B
Refers to extern alias
with namespace alias
Uses
namespace
alias
C
197 Pragma directives
Console.WriteLine(typeof(SecondAlias::Demo.Example));
Console.ReadLine();
}
}
The code in listing 7.8 is quite straightforward, and demonstrates that you can
either use extern directives directly D or via namespace aliases (B and C).


Pages:
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386