English 中文(简体)
Namespaces, aliases and Visual Studio Forms Designer
原标题:

I m having a problem with conflicting namespaces and code that gets autogenerated by the forms designer in Visual Studio 2008. I have search many forums and different documentation, but have not been able to find any solution to this problem.

I have one assembly called Foo.dll with the following namespace/code:

namespace Foobar.System
{
    public class MySystemClass() { }
}

Then, I have another assembly which contains som commonly used forms:

namespace Foobar.MyCommonForms
{
    public class MyForm : System.Windows.Forms.Form
    {
        public void SomeMethod()
        {
            var systemclass = new Foobar.System.MySystemClass();
        }
    }
}

Here, the compilers display the following error: Type or namespace Windows is not part of namespace Foobar.System . Obviously, the compiler tries to look for the class System.Windows.Forms.Form in namespace Foobar.System.Windows.Forms!

I have been able to solve this by using the alias x instead of global when referencing to the assembly Foo.dll, and declaring extern alias x in my code files, and put x:: in front of every reference to types and classes in the namespace Foobar.System. The code compiles.

But it seems that the forms designer don t recognise this, and gives me an error when trying to display the form. This, again, can be solved by manually putting global:: in front of every reference to classes in System.Windows.Forms (e.g. global::System.Windows.Forms.Button), but every time chances are made to the form, the code is automaticaly re-generated, and the global:: part is removed.

So, the question is: Is there a way to make the forms designer aware of the alias x that is used to reference my assembly Foo.dll, or is there another, better solution to this? Renaming the namespace Foobar.System to something else is just too much work.

问题回答

There s no way around this, from what I can tell.

The popular refactoring tools such as Resharper or Refactor! both include the ability to globally rename a namespace. I d seriously consider using those.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签