Is it possible to do static partial classes?

瘦欲@ 提交于 2019-12-23 06:44:41

问题


I want to take a class I have and split it up into several little classes so it becomes easier to maintain and read. But this class that I try to split using partial is a static class.

I saw in an example on Stackoverflow that this was possible to do but when I do it, it keeps telling me that I cannot derive from a static class as static classes must derive from object.

So I have this setup:

public static class Facade
{
    // A few general methods that other partial facades will use
}

public static partial class MachineFacade : Facade
{
    // Methods that are specifically for Machine Queries in our Database
}

Any pointers? I want the Facade class to be static so that I don't have to initialize it before use.


回答1:


Keep naming and modifiers consistent across files:

public static partial class Facade
{
    // A few general methods that other partial facades will use
}

public static partial class Facade
{
    // Methods that are specifically for Machine Queries in our Database
}



回答2:


The problem is not that the class is a partial class. The problem is that you try to derive a static class from another one. There is no point in deriving a static class because you could not make use Polymorphism and other reasons for inheritance.

If you want to define a partial class, create the class with the same name and access modifier.




回答3:


You can not inherit a static class.

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.




回答4:


you do not need to override anything, just give them the same name:

public static partial class Facade
{
    // this is the 1st part/file
}

public static partial class Facade
{
    // this is the 2nd part/file
}



回答5:


C# doesn't support inheritance from a static class.

You have to choose between your classes being static:

public static class Facade
{
    // A few general methods that other partial facades will use
}

public static partial class MachineFacade
{
    // Methods that are specifically for Machine Queries in our Database
}

...or whether you wish MachineFacade to derive from Facade:

public class Facade
{
    // A few general methods that other partial facades will use
}

public partial class MachineFacade : Facade
{
    // Methods that are specifically for Machine Queries in our Database
}



回答6:


Although I am too late for the party...
According to your problem description, I think your goals are:

  1. Want a static method group.
  2. Want a sub-group of it to be specialized for certain domain.

In this case, I would suggest using nested classes:

public static class Facade
{
    // some methods

    public static class Machine
    {
        // some other methods
    }
}

Pros:

  1. Methods are static. You can use them directly without initialize any instances.
  2. The nested class names are used like namespaces. Readers of your code knows clearly that Facade.Machine methods are more specific than Facade ones and their target domain.
  3. You can make methods with the same signature in different class level doing different things to mimic method overriding.

Cons:

  1. There is no way to forbids users calling Facade.Machine methods when they should not.
  2. No inheritance. Facade.Machine does not automatically have methods from Facade.


来源:https://stackoverflow.com/questions/35549029/is-it-possible-to-do-static-partial-classes

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