简体   繁体   English

不同命名空间中的部分类

[英]Partial class in different namespaces

Can I create partial class in different namespaces?我可以在不同的命名空间中创建分部类吗? Will it work correct?它会正常工作吗? ex:前任:

class1.cs类1.cs

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

class2.cs 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) (如果我的例子不正确)

A class's name includes it's namespace, so name1.Foo and name1.name2.Foo are two completely separate types.一个类的名称包括它的命名空间,所以name1.Fooname1.name2.Foo是两个完全独立的类型。 So the short answer to your question is: No.所以对你的问题的简短回答是:不。

Why do you need to do something like this?为什么你需要做这样的事情?

Partial class is only possible in same namespace and same assembly.部分类只能在相同的命名空间和相同的程序集中。

Namespace could be in two different assemblies but partial class could not.命名空间可以在两个不同的程序集中,但部分类不能。

Here are some point to consider while implementing the partial classes:-以下是实现部分类时需要考虑的一些要点:-

  • Use partial keyword in each part of partial class.在partial 类的每个部分使用partial 关键字。

  • 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.分部类的每个部分都应该在同一个程序集或 DLL 中,换句话说,您不能在不同类库项目的源文件中创建分部类。

  • 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.如果部分类的一部分是抽象的,那么整个类将被视为抽象类。

This will not work.这是行不通的。 The compiler will give you an ambiguous name error on the Foo classFoo = new Foo();编译器会在Foo classFoo = new Foo();上给你一个不明确的名称错误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.要使分部类工作,它们必须在同一个命名空间中,因为命名空间实际上是类型的完全限定名称的一部分。

Also, for static classes you can implement something like this with the help of fresh C# 6.0 using static feature .此外,对于静态类,您可以在新的 C# 6.0 的帮助下使用静态功能实现类似的功能

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.但正如其他人所注意到的那样,部分类不是方法。

MSDN https://msdn.microsoft.com/en-us/library/wa80x488.aspx对部分类和方法的限制

I am assuming your main goal was to distribute the methods amongst different namespaces, otherwise it would have been trivial (put everything in one class whether partial or not and you're done).我假设您的主要目标是在不同的命名空间之间分配方法,否则这将是微不足道的(将所有内容都放在一个类中,无论是否部分,您就完成了)。

So the assumed objectives are:所以假设的目标是:

  • Have the 2 methods Bar1 in namespace name1 and Bar2 in namespace name1.name2有2种方法Bar1在命名空间name1Bar2在命名空间name1.name2
  • Be able to invoke any of the above methods in the context of one class, here ClsFoo能够在一个类的上下文中调用上述任何方法,这里是ClsFoo

You can't achieve this with partial classes, but you can achieve it in a different way: if you use extension methods and bind them to a particular class, here ClsFoo , then you can do the following:你不能用部分类来实现这一点,但你可以用不同的方式来实现:如果你使用扩展方法并将它们绑定到一个特定的类,这里是ClsFoo ,那么你可以执行以下操作:

using SomeOtherNamespace;
using name1;
using name1.name2;

namespace mainClass
{
    public static class mainClass
    {
        public static void Main()
        {
            var classFoo = new ClsFoo();
            var count = classFoo.Bar1() + classFoo.Bar2();
            Console.WriteLine($"count = {count}"); // output is 110
        } // main               
    } // class
} // namespace

namespace SomeOtherNamespace
{
    public class ClsFoo
    {
        // does not need to contain any code
    } // class
} // namespace

namespace name1
{
    public static class FooExt
    {
        public static int Bar1(this ClsFoo foo)
        {
            return 10;
        } // method
    } // class
} // namespace

namespace name1.name2
{
    public static class FooExt
    {
        public static int Bar2(this ClsFoo foo)
        {
            return 100;
        } // method
    } // class
} // namespace

Run it online在线运行

This way, you declare an empty class ClsFoo and then write some extension methods Bar1() and Bar2() , which reside in different namespaces and static extension classes.这样一来,你声明一个空的类ClsFoo然后写一些扩展方法Bar1()Bar2()它驻留在不同的命名空间和静态扩展类。

Note: The extension classes may have the same name FooExt as long as they are in different namespaces, of course you can also give them different names like FooExt1 and FooExt2 if you like - and the example will still work;注意:扩展类可以有相同的名称FooExt ,只要它们在不同的命名空间中,当然你也可以给它们不同的名称,比如FooExt1FooExt2如果你喜欢 - 这个例子仍然有效; even in older versions of C#.即使在旧版本的 C# 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM