How to create a namespace containing a "System" in .NET without conflict?

Sometimes I created a namespace in C # (I don’t know if the problem is the same in VB.NET) containing "System", and when I turn it on from another 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 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 it’s good for you :) I’m sure that many have seen this problem.

I'm not quite sure why he does it. This will even happen in files, such as generated code for web services, that does not contain links to RR.System .

All this happens only because I include the RR.System DLL in another project.

How can i avoid this? Or fix it?

+4
source share
9 answers

I still don't understand why the child namespace conflicts with the root namespace? All types under the namespace can be fully qualified, and fully qualified names are of different types. eg.

 System.Abc.Xyz.Type 

has nothing to do with

 Abc.Xyz.System.Type 

System in the first case refers to a completely different concept (the name of the company under the leadership), while System in the second case can refer to the name of the product or subsystem.

If root namespaces can cause such interference, then this is undoubtedly a big problem, because I can choose to call my new Amazon rainforest monitoring product and put all my types under MyCompany.Amazon . Then later, I can choose to save my data using the S3 repository, and suddenly the Amazon namespace causes a conflict.

We faced the same problem as our project, which is divided into 3 main subsystems: database, user and system. They seem obvious child namespaces in our root namespace MyCompany.

Remember that this has nothing to do with using operators, as Simon said: “This will happen even in files, such as generated code for web services that does not contain a link to RR.System”

UPDATE: The next stack overflow question goes along the same lines. However, the MSDN article points to a discussion of the name of a class called System that hides the namespace (fairly fair), and the use of System as a top-level namespace (fair enough). However, he does not discuss why the child namespace conflicts with the root.

Stack Overflow Q: Is Global: Bad code smell in C #?
MSDN Article: A Practical Guide. Using the Namespace Alias ​​Qualifier

+4
source

Odd.

Now, why are you calling your System project?

+6
source

To avoid confusion, you can fully qualify your namespace references:

 global::System.ServiceModel 

and etc.

+6
source

It is not possible to refer to both namespaces using the shorthand method. You will either have to rename your class to prevent a collision, or an alias of your class (so you will need to change your references in your code to use an alias) ...

 Using System; // The namespace seen and used in all .cs files Using Sys = RR.System; // Just replace -your- 'System' references with 'Sys' 

Although this method is legal in C #, it is messy and suggests renaming your reference class.

+1
source

It reminded me of an old joke - The compiler hurts when I do this

+1
source

If you have the option, you may want to rename your namespace to something like SystemUtilities or the like, or you can fully qualify all the other links, which can be a serious pain. Ambiguity with BCL can lead to some nasty looking code.

0
source

If your project contains links to both the system and your own library (RR.System), the compiler will have an ambiguous reference for sorting. He is not sure which one you want.

You can always use aliasing to make sure your code explicitly references the correct code from your project.

By the way, there is a wealth of information about best practices that should be followed from Brad Abrams in the Wireframe Design Guide .

0
source

Namespaces in the main projects of my companies are divided into several levels:

Company.au.ProductName.GUI. *
Company.au.ProductName.Data. *
...

where * will be further broken down depending on the function

0
source

My company uses Company.Group.Platform.Application.Layer.Component. * It is very annoying and confusing. Needless to say that I use aliases

0
source

All Articles