Partial class in different namespaces

空扰寡人 提交于 2019-11-26 12:45:57

问题


Can I create partial class in different namespaces? Will it work correct? e.x.:

class1.cs

namespace name1
{
    public partial class Foo
    {
        Bar1(){
            return 10;
        }
    }
}

class2.cs

namespace name1.name2
{
    public partial class Foo
    {
        Bar2(){
            return 100;
        }
    }
}

main.cs

using name1;
using name1.name2;

namespace mainClass
{
    public class mainClass
    {
        Foo classFoo = new Foo();
        int Count = classFoo.Bar1() + classFoo.Bar2();
        // Will Count = 110?
    }
}

What should I do to make it work? (if my example not correct)


回答1:


A class's name includes it's namespace, so name1.Foo and name1.name2.Foo are two completely separate types. So the short answer to your question is: No.

Why do you need to do something like this?




回答2:


Partial class is only possible in same namespace and same assembly.

Namespace could be in two different assemblies but partial class could not.




回答3:


Here are some point to consider while implementing the partial classes:-

  • Use partial keyword in each part of partial class.

  • Name of each part of partial class should be the same but source file name for each part of partial class can be different.

  • All parts of a partial class should be in the same namespace.

  • Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project.

  • Each part of a partial class has the same accessibility. (like private, public or protected)

  • If you inherit a class or interface on a partial class then it is inherited on all parts of a partial class.

  • If a part of a partial class is sealed then the entire class will be sealed.

  • If a part of partial class is abstract then the entire class will be considered an abstract class.




回答4:


This will not work. The compiler will give you an ambiguous name error on the Foo classFoo = new Foo(); line. For partial classes to work, they must be in the same namespace because the namespace is actually part of the fully qualified name of the type.




回答5:


Also, for static classes you can implement something like this with the help of fresh C# 6.0 using static feature.

Consider:

namespace SomeLogic1
{
    public static class Util
    {
        public static int Bar1()
        {
            return 1;
        }
    }
}

namespace SomeLogic2
{
    public static class Util
    {
        public static int Bar2()
        {
            return 2;
        }
    }
}

namespace GeneralStuff
{
    using SomeLogic1;
    using SomeLogic2;

    public class MainClass
    {
        public MainClass()
        {
            // Error CS0104
            // 'Util' is an ambiguous reference between 'SomeLogic1.Util' and 'SomeLogic2.Util'
            var result = Util.Bar1() + Util.Bar2(); 
        }
    }
}  

Right, that does not compile, the error message is clear. To fix the situation you can directly specify namespaces (but you don't want this as far as I understand):

namespace GeneralStuff
{
    public class MainClass
    {
        public MainClass()
        {
            var result = SomeLogic1.Util.Bar1() + SomeLogic2.Util.Bar2(); 
        }
    }
}

OR you can apply using static feature this way:

namespace GeneralStuff
{
    using static SomeLogic1.Util;
    using static SomeLogic2.Util;

    public class MainClass
    {
        public MainClass()
        {
            var result = Bar1() + Bar2(); 
        }
    }
}

Perhaps it is ok to do this for some helper/utils classes. But partial classes are not the way, as other have noticed.




回答6:


Restrictions on partial classes and method from MSDN https://msdn.microsoft.com/en-us/library/wa80x488.aspx



来源:https://stackoverflow.com/questions/4504288/partial-class-in-different-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!