Referencing assemblies

C

caelis

Hi,

I've got a problem with referencing assemblies.

I reference an assembly in my programm; this assembly defines a method
"SayHello" in the class "Hello", namespace "MyNamespace". In my program
I define a method called "SayHello" in a class called "Hello",
namespace "MyNamespace", too. When I try to call the method
"MyNamespace.Hello.SayHello();" the C#-compiler outputs a warning since
it doesn't know whether to call the method defined in my programm or
the method defined in the referenced assembly (it chooses the method
defined in my programm by default, btw.).

With IL (MSIL) I got the possibility to choose myself which method to
call:

//Calls the method defined in my program
call void MyNamespace.Hello::SayHello()

//Calls the method defined in the referenced assembly
call void [externalAssembly] MyNamespace.Hello::SayHello()

Is something similar also possible with C#?

Thanks,
Simon
 
B

Brendan Green

Why would you ever want to do this?

Is it possible using an alias with the using directive?

using OtherMyNamespace = MyNamespace

class MyNamespace
{
// and so on and so forth...
}
 
J

Jon Shemitz

caelis said:
I reference an assembly in my programm; this assembly defines a method
"SayHello" in the class "Hello", namespace "MyNamespace". In my program
I define a method called "SayHello" in a class called "Hello",
namespace "MyNamespace", too. When I try to call the method
"MyNamespace.Hello.SayHello();" the C#-compiler outputs a warning since
it doesn't know whether to call the method defined in my programm or
the method defined in the referenced assembly (it chooses the method
defined in my programm by default, btw.).
call void MyNamespace.Hello::SayHello()
call void [externalAssembly] MyNamespace.Hello::SayHello()
Is something similar also possible with C#?

In 2.0, you can specify an alias reference (reference properties, in
VS.2005) so that your externalAssembly might be loaded into the
externalAssembly namespace, and you can say

extern alias externalAssembly;

and then refer to externalAssembly::MyNamespace.Hello::SayHello().
 
J

Jon Skeet [C# MVP]

caelis said:
I've got a problem with referencing assemblies.

I reference an assembly in my programm; this assembly defines a method
"SayHello" in the class "Hello", namespace "MyNamespace". In my program
I define a method called "SayHello" in a class called "Hello",
namespace "MyNamespace", too. When I try to call the method
"MyNamespace.Hello.SayHello();" the C#-compiler outputs a warning since
it doesn't know whether to call the method defined in my programm or
the method defined in the referenced assembly (it chooses the method
defined in my programm by default, btw.).

Well, the first thing to suggest is: don't do that. Even if you can be
explicit about it, it's never going to be a nice thing to do in terms
of readability.
With IL (MSIL) I got the possibility to choose myself which method to
call:

//Calls the method defined in my program
call void MyNamespace.Hello::SayHello()

//Calls the method defined in the referenced assembly
call void [externalAssembly] MyNamespace.Hello::SayHello()

Is something similar also possible with C#?

There is in C# 2.0. You can use use "extern aliases":

extern alias OtherLib;

class Foo
{
...
OtherLib::MyNamespace.Hello.SayHello();
}

You would then compile from the command line with:

csc /r:Otherlib=WhateverTheDllNameIs.dll Foo.cs

However, I don't know off-hand how you do this in VS 2005.
 
C

caelis

Thanks very much for your answers. "extern alias" is exactly what I was
looking for. Visual C# 2005 even has an "Alias" field in the "Reference
Properties" pane - I overlooked that before.

Bye,
Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top