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


当前回答

大多数人认为partial只应该用于生成了代码文件的类或接口。我不同意,原因如下。

举个例子,让我们看看c#系统。数学课…这是类。我不会尝试把70多个方法都塞进同一个代码文件中。维持它将是一场噩梦。

将每个数学方法放入单独的部分类文件中,并将所有代码文件放入项目中的math文件夹中,将显著地使组织更加干净。

同样的道理也适用于许多其他具有大量不同功能的类。例如,管理PrivateProfile API的类可以通过在单个项目文件夹中拆分为一组干净的部分类文件而受益。

就我个人而言,我还将大多数人所说的“助手”或“实用工具”类划分为每个方法或方法功能组的单独部分文件。例如,在一个项目中,字符串助手类几乎有50个方法。即使使用区域,这也将是一个冗长而笨拙的代码文件。为每个方法使用单独的部分类文件维护要容易得多。

我只会小心使用部分类,并在整个项目中保持所有代码文件布局一致。例如,将任何类公共枚举和类私有成员放入文件夹中的Common.cs或类似名称的文件中,而不是将它们分散到文件中,除非它们只特定于包含它们的部分文件。

请记住,当您将一个类拆分为单独的文件时,您也失去了使用文本编辑器拆分栏的能力,该功能可以让您同时查看当前文件的两个不同部分。

其他回答

部分类最近帮助了源代码控制,多个开发人员在一个文件中添加新方法,并将其添加到文件的同一部分(由Resharper自动化)。

这些推入git会导致合并冲突。我发现没有办法告诉合并工具把新方法作为一个完整的代码块。

在这方面,部分类允许开发人员坚持他们文件的一个版本,并且我们可以稍后手工将它们合并回去。

的例子,

cs—保存字段、构造函数等 MainClass1.cs—开发人员实现时的新代码 cs -是另一个用于新代码的开发人员类。

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.

从MSDN:

1.在编译时,合并部分类型定义的属性。例如,考虑以下声明:

[SerializableAttribute]
partial class Moon { }

[ObsoleteAttribute]
partial class Moon { }

它们等价于以下声明:

[SerializableAttribute]
[ObsoleteAttribute]
class Moon { }

以下是所有部分类型定义的合并:

XML注释 接口 泛型类型参数属性 类属性 成员

2.另外,嵌套的分部类也可以是分部类:

partial class ClassWithNestedClass
{
    partial class NestedClass { }
}

partial class ClassWithNestedClass
{
    partial class NestedClass { }
}

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

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

——核心逻辑

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

产品的方法

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

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

我知道这个问题很老了,但我想补充一下我对部分课程的看法。

我个人使用部分类的一个原因是当我为程序创建绑定时,尤其是状态机。

例如,OpenGL是一个状态机,有很多方法都可以全局更改,然而,根据我的经验,绑定类似于OpenGL的东西,其中有很多方法,类很容易超过10k LOC。

部分类将为我分解这个问题,并帮助我快速找到方法。