How to have a class in a namespace that has the same name that is part of another namespace in a .net class library
ok...this gets confusing for me to explain, so i will show a class from one namespace and a seperate namespace...
Company.Product.Domain
Company.Product.Domain.Data.Contracts
If I am in the Company.Product.Domain
class and I try to use the classes within Company.Product.Domain.Data.Contracts
, when I attempt to type this out, i get to Company.Product.Domain
and will not show intellisense any further. It also shows that that reference is a class instead of allowing me to get to the namespace.
I do have the Company.Product.Domain.Data.Contracts
referenced in my Company.Product.Domain
class.
Is it possible to have a namespace Company.Product
with a class inside of it and also another namespace of Company.Product.Domain.Data.Contracts
? If so, how?
Thanks
Yes, it's possible. However, you should avoid it like the plague. It makes everything much more complicated.
Eric Lippert has written a series of blog posts on this very topic recently. Admittedly it's not quite the same issue - he's talking about Foo.Bar.Bar
(where Foo.Bar
is the namespace, and Bar
is the class) but I believe you'll run into many of the same issues. Indeed, I think you may actually end up making them worse than the scenario he's describing. Please don't do it.
- Part One: Collisions amongst referenced assemblies
- Part Two: Machine-generated code
- Part Three: Bad hierarchical design
- Part Four: Making the problem worse
You can alias your Company.Product.Domain.Data namespace.
So in your Domain class, instead of
using Company.Product.Domain.Data;
try something like
using myData = Company.Product.Domain.Data;
namespace Company.Product
{
class Domain
{ ...
myData::Data.method();
}
}
I'm not positive that intellisense behaves the way you want with this, but it does disambiguate the Domain class vs. Domain namespace.
精彩评论