Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a namespace containing 'System' in .NET without it conflicting?

Tags:

c#

.net

Sometimes I've made a namespace in C# (I don't know if the problem is the same in VB.NET) containing 'System' and when I include it from a different DLL it goes crazy and conflicts with everything containing 'System'. This leads to crazy errors such as the following :

The type or namespace name 'ServiceModel' does not exist in the namespace 'RR.System'

The type or namespace name 'Runtime' does not exist in the namespace 'RR.System'

The type or namespace name 'SerializableAttribute' does not exist in the namespace 'RR.System'

If you don't know what I'm talking about then good for you :) I'm sure many have seen this issue.

I'm not completely sure why it does this. It will occur even in files, such as generated code for web services that doesn't contain any reference to RR.System.

This all occurs just because I'm including RR.System the DLL in a different project.

How can I avoid this happening? Or fix it?

like image 437
Simon_Weaver Avatar asked Dec 02 '22 08:12

Simon_Weaver


2 Answers

I still don't see why a child namespace conflicts with a root namespace? All types under a namespace can be fully qualified, and the fully qualified names refer to different types. e.g.

System.Abc.Xyz.Type

has nothing in relation to

Abc.Xyz.System.Type

The System in the first case refers to a completely different concept (The Company name under the guidelines), whereas the System in the second case could refer to the product or subsystem name.

If root namespaces can cause this kind of interference then surely that's a big problem because I may choose to call my new rainforest monitoring product Amazon and put all my types under MyCompany.Amazon. Then later on I may choose to store my data using the S3 storage and suddenly the namespace Amazon causes a conflict.

We've just run into the same issue as our project is split into 3 major sub-systems - Database, User and System. These seem like obvious child namespaces under our MyCompany root namespace.

Remember, this has nothing to do with Using statements as Simon said "It will occur even in files, such as generated code for web services that doesn't contain any reference to RR.System"

UPDATE: The following Stack Overflow question is along the same lines. However the MSDN article it points to discusses a class name called System hiding a namespace (fair enough) and also using System as a top-level namespace (fair enough). However it does not discuss why a child namespace conflicts with a root one.

Stack Overflow Q: Is global:: a bad code smell in C#?
MSDN Article: How to: Use the Namespace Alias Qualifier

like image 51
Chris Oldwood Avatar answered Dec 04 '22 21:12

Chris Oldwood


Odd.

Now, why are you calling your project "System"?

like image 42
yfeldblum Avatar answered Dec 04 '22 21:12

yfeldblum