我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。


当前回答

通过部分类,只需添加源文件,就可以向适当设计的程序添加功能。例如,可以设计一个文件导入程序,这样就可以通过添加处理文件的模块来添加不同类型的已知文件。例如,主文件类型转换器可以包含一个小类:

Partial Public Class zzFileConverterRegistrar
    Event Register(ByVal mainConverter as zzFileConverter)
    Sub registerAll(ByVal mainConverter as zzFileConverter)
        RaiseEvent Register(mainConverter)
    End Sub
End Class

每个希望注册一个或多个类型的文件转换器的模块可以包括如下内容:

Partial Public Class zzFileConverterRegistrar
    Private Sub RegisterGif(ByVal mainConverter as zzFileConverter) Handles Me.Register
        mainConverter.RegisterConverter("GIF", GifConverter.NewFactory))
    End Sub
End Class

注意,主文件转换器类并没有“公开”——它只是公开了一个小存根类,外接程序模块可以钩子到这个存根类。存在命名冲突的轻微风险,但如果每个外接程序模块的“寄存器”例程是根据它处理的文件类型命名的,那么它们可能不会造成问题。如果担心这样的事情,可以在注册子例程的名称中插入GUID。

Edit/Addendum To be clear, the purpose of this is to provide a means by which a variety of separate classes can let a main program or class know about them. The only thing the main file converter will do with zzFileConverterRegistrar is create one instance of it and call the registerAll method which will fire the Register event. Any module that wants to hook that event can execute arbitrary code in response to it (that's the whole idea) but there isn't anything a module could do by improperly extending the zzFileConverterRegistrar class other than define a method whose name matches that of something else. It would certainly be possible for one improperly-written extension to break another improperly-written extension, but the solution for that is for anyone who doesn't want his extension broken to simply write it properly.

在不使用分部类的情况下,可以在主文件转换器类的某个地方写一些代码,如下所示:

  RegisterConverter("GIF", GifConvertor.NewFactory)
  RegisterConverter("BMP", BmpConvertor.NewFactory)
  RegisterConverter("JPEG", JpegConvertor.NewFactory)

但是添加另一个转换器模块需要进入转换器代码的那一部分,并将新的转换器添加到列表中。使用partial方法,这就不再需要了——所有转换器都会自动包含。

其他回答

Multiple Developer Using Partial Classes multiple developer can work on the same class easily. Code Generator Partial classes are mainly used by code generator to keep different concerns separate Partial Methods Using Partial Classes you can also define Partial methods as well where a developer can simply define the method and the other developer can implement that. Partial Method Declaration only Even the code get compiled with method declaration only and if the implementation of the method isn't present compiler can safely remove that piece of code and no compile time error will occur. To verify point 4. Just create a winform project and include this line after the Form1 Constructor and try to compile the code partial void Ontest(string s);

以下是在实现分部类时需要考虑的几点

Use partial keyword in each part of partial class. The name of each part of partial class should be the same but the 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 from a different class library project. Each part of a partial class must have the same accessibility. (i.e: private, public or protected) If you inherit a class or interface on a partial class then it is inherited by all parts of that 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.

除了其他答案之外……

我发现它们可以作为重构神类的垫脚石。如果一个类有多个职责(特别是如果它是一个非常大的代码文件),那么我发现为每个职责添加1x个部分类作为组织和重构代码的第一步是有益的。

这非常有帮助,因为它可以帮助使代码更具可读性,而不会实际影响执行行为。它还可以帮助确定何时易于重构某个职责,或者何时与其他方面紧密纠缠在一起。

然而,需要明确的是,这仍然是糟糕的代码,在开发结束时,您仍然希望每个类有一个职责(而不是每个分部类)。这只是一块垫脚石:)

作为预编译器指令的替代方案。

如果您使用预编译器指令(即# If DEBUG),那么您最终会看到一些看起来粗糙的代码与实际的Release代码混合在一起。

你可以创建一个单独的分部类来包含这些代码,或者将整个分部类包装在一个指令中,或者省略该代码文件,使其不被发送给编译器(实际上是做同样的事情)。

每当我有一个类,其中包含一个具有重要大小/复杂性的嵌套类时,我将该类标记为partial,并将嵌套类放在一个单独的文件中。我使用规则命名包含嵌套类的文件:[class name]。[嵌套类名].cs。

下面的MSDN博客解释了如何使用带有嵌套类的部分类来实现可维护性:http://blogs.msdn.com/b/marcelolr/archive/2009/04/13/using-partial-classes-with-nested-classes-for-maintainability.aspx

如果您有一个足够大的类,而不适合进行有效的重构,那么将它分离到多个文件中有助于保持内容的组织性。

例如,如果您有一个包含论坛和产品系统的站点数据库,并且您不想创建两个不同的提供者类(与代理类不是一回事,只是为了清楚起见),您可以在不同的文件中创建单个分部类,例如

——核心逻辑

cs -专门与论坛相关的方法

产品的方法

这是另一种让事情井井有条的方法。

另外,正如其他人所说,这是向生成的类添加方法而不会在下次重新生成类时破坏所添加的方法的唯一方法。这对于模板生成的(T4)代码、orm等非常方便。