.NET Multiple Namespaces for single class

前端 未结 3 1630
一生所求
一生所求 2020-12-18 19:02

Is it possible to have a single class reside within two name-spaces and how can I do this?

To clarify: We have a class library (let say root namespace is classLib1),

相关标签:
3条回答
  • 2020-12-18 19:26

    As a work around you can 'using' the class into the second namespace when you need it:

    namespace classLib1.section2
    {
        public myBigClass
        {
    

    and in every file which uses it in the old namespace you can add one line

    namespace classLib1.section1
    {
        using myBigClass = classLib1.section2.myBigClass;
    

    as a temporary patch-up until you've fixed this properly.

    0 讨论(0)
  • 2020-12-18 19:39

    No, there is no way to give a single class two names (the namespace is actually just a part of the class name).

    As a workaround, you could move the classes to their new location, and create thin wrappers around them at the old location (Facade Pattern). A better solution, of course, would be to move the classes and fix the legacy code accordingly.

    0 讨论(0)
  • 2020-12-18 19:40

    Type Forwarding was introduced in .net 2.0, which means you can use the TypeForwardedToAttribute attribute to indicate that a type which was originally present in AssemblyX is now to be found in AssemblyY.

    I'm fairly sure this is only appropriate/applicable if you're separating out into multiple assemblies, but is worth knowing even if not.

    0 讨论(0)
提交回复
热议问题