471,327 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 software developers and data experts.

Referencing assemblies

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

Jan 16 '06 #1
4 2399
caelis wrote:
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().

--
<http://www.midnightbeach.com>
Jan 16 '06 #2
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...
}

"caelis" <si********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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

Jan 16 '06 #3
caelis <si********@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 16 '06 #4
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

Jan 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Mark Broadbent | last post: by
16 posts views Thread by PromisedOyster | last post: by
9 posts views Thread by Brett Romero | last post: by
reply views Thread by Demetri | last post: by
5 posts views Thread by =?Utf-8?B?U3RlZmFuIEJhcmxvdw==?= | last post: by
1 post views Thread by Dave Anson | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.