我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。
当前回答
一个很好的用法是将生成的代码与属于同一类的手写代码分离。
例如,由于LINQ to SQL使用部分类,您可以编写自己的某些功能片段的实现(如多对多关系),并且当您重新生成代码时,这些自定义代码片段不会被覆盖。
WinForms代码也是如此。所有设计器生成的代码都放在一个文件中,您通常不会接触这个文件。手写的代码放到另一个文件中。这样,当您在设计器中更改某些内容时,您所做的更改不会被影响。
其他回答
另一个用途是分割不同接口的实现,例如:
partial class MyClass : IF3
{
// main implementation of MyClass
}
partial class MyClass : IF1
{
// implementation of IF1
}
partial class MyClass : IF2
{
// implementation of IF2
}
部分类最近帮助了源代码控制,多个开发人员在一个文件中添加新方法,并将其添加到文件的同一部分(由Resharper自动化)。
这些推入git会导致合并冲突。我发现没有办法告诉合并工具把新方法作为一个完整的代码块。
在这方面,部分类允许开发人员坚持他们文件的一个版本,并且我们可以稍后手工将它们合并回去。
的例子,
cs—保存字段、构造函数等 MainClass1.cs—开发人员实现时的新代码 cs -是另一个用于新代码的开发人员类。
通过部分类,只需添加源文件,就可以向适当设计的程序添加功能。例如,可以设计一个文件导入程序,这样就可以通过添加处理文件的模块来添加不同类型的已知文件。例如,主文件类型转换器可以包含一个小类:
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.