Sonntag, 19. August 2012

Namespace Problems in C#

Hallo, just stumbled over two problems with namespaces in C#

First things first:

The type name 'IO' does not exist in the type 'Microsoft.Office.Interop.Word.System' when you have the following usings defined:
    using System.IO;
    using Microsoft.Office.Interop.Word;
The problem is that there exists a namespace Microsoft.Office.Interop.Word.System and Visual Studio or MSBuild somehow apply the System.*-namespaces (even they're defined before) to Micorosft.Office.Interop.Word and make it Microsoft.Office.Interop.Word.System.*

There is also a StackOverflow question about it, but unfortunately without any answer. My solution to this was more of a workaround, I named the namespace Microsoft.Office.Interop.Word.
    using System.IO;
    using word = Microsoft.Office.Interop.Word;
The downside of this workaround is that you need to use "word" in front of every type from the namespace. But at least the code compiles.

The second problem...

was a bit my fault but also not very clear in the beginning.
I have 2 Projects:
  • Common (for common functions, ExtensionMethods, ... which I may use also in the future)
  • Editor - the actual project
in Editor I had another folder "Common" which lead to the namespace Editor.Common

Now, the 1 Million $ question - how do you use in a class which has the namespace Editor.Common a class from the project and namespace Common? - The answer is simple: you can't.

And the moral of this story - don't mix your namespaces and give them names which are as unique as possible.


Happy Coding!
Peter

Keine Kommentare:

Kommentar veröffentlichen