开发者

Can I hide classes in the global namespace in C#?

开发者 https://www.devze.com 2023-01-14 11:53 出处:网络
In a visual studio 2008 solution, I have two versions of the same class in two different namespaces. In VB, if I do this:

In a visual studio 2008 solution, I have two versions of the same class in two different namespaces.

In VB, if I do this:

imports MyNamespace
' ...
dim x as DuplicatedClass = new DuplicatedClass()
开发者_开发问答

it uses MyNamespace.DuplicatedClass instead of the globally-namespaced DuplicatedClass. Meanwhile, in C#, doing this:

using MyNamespace;
// ...
DuplicatedClass x = new DuplicatedClass();

uses the globally-namespaced DuplicatedClass. To use the other version, I have to use MyNamespace.DuplicatedClass.

I realize this is a problematic setup, but I can't change it. Is there a way to prevent C# from seeing the globally namespaced class, or to specifically un-load it, or...? Given how many classes are in the global namespace, being forced to choose the namespace every time could get pretty time-costly.


Perhaps the best you could do is create a using alias:

//create alias
using defDuplicatedClass = MyNamespace.DuplicatedClass;

defDuplicatedClass x = new defDuplicatedClass();

The alias is file scoped. So you'd have to repeat it at the top of each file as needed, but perhaps that is better than repeating a namespace with every occurance of DuplicatedClass.

0

精彩评论

暂无评论...
验证码 换一张
取 消