我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。
当前回答
每当我有一个类,其中包含一个具有重要大小/复杂性的嵌套类时,我将该类标记为partial,并将嵌套类放在一个单独的文件中。我使用规则命名包含嵌套类的文件:[class name]。[嵌套类名].cs。
下面的MSDN博客解释了如何使用带有嵌套类的部分类来实现可维护性:http://blogs.msdn.com/b/marcelolr/archive/2009/04/13/using-partial-classes-with-nested-classes-for-maintainability.aspx
其他回答
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.
一个很好的用法是将生成的代码与属于同一类的手写代码分离。
例如,由于LINQ to SQL使用部分类,您可以编写自己的某些功能片段的实现(如多对多关系),并且当您重新生成代码时,这些自定义代码片段不会被覆盖。
WinForms代码也是如此。所有设计器生成的代码都放在一个文件中,您通常不会接触这个文件。手写的代码放到另一个文件中。这样,当您在设计器中更改某些内容时,您所做的更改不会被影响。
分部类的主要用途是生成代码。如果你看看WPF (Windows Presentation Foundation)网络,你用标记(XML)定义你的UI。该标记被编译成部分类。您可以用自己的部分类填充代码。
大多数人认为partial只应该用于生成了代码文件的类或接口。我不同意,原因如下。
举个例子,让我们看看c#系统。数学课…这是类。我不会尝试把70多个方法都塞进同一个代码文件中。维持它将是一场噩梦。
将每个数学方法放入单独的部分类文件中,并将所有代码文件放入项目中的math文件夹中,将显著地使组织更加干净。
同样的道理也适用于许多其他具有大量不同功能的类。例如,管理PrivateProfile API的类可以通过在单个项目文件夹中拆分为一组干净的部分类文件而受益。
就我个人而言,我还将大多数人所说的“助手”或“实用工具”类划分为每个方法或方法功能组的单独部分文件。例如,在一个项目中,字符串助手类几乎有50个方法。即使使用区域,这也将是一个冗长而笨拙的代码文件。为每个方法使用单独的部分类文件维护要容易得多。
我只会小心使用部分类,并在整个项目中保持所有代码文件布局一致。例如,将任何类公共枚举和类私有成员放入文件夹中的Common.cs或类似名称的文件中,而不是将它们分散到文件中,除非它们只特定于包含它们的部分文件。
请记住,当您将一个类拆分为单独的文件时,您也失去了使用文本编辑器拆分栏的能力,该功能可以让您同时查看当前文件的两个不同部分。
下面列出了部分类的一些优点。
您可以分离UI设计代码和业务逻辑代码,以便易于阅读和理解。例如,你正在使用Visual Studio开发一个web应用程序,并添加一个新的web表单,然后有两个源文件,“aspx.cs”和“aspx.designer.cs”。这两个文件具有具有partial关键字的相同类。".aspx.cs"类有业务逻辑代码,而"aspx.designer.cs"有用户界面控件定义。
When working with automatically generated source, the code can be added to the class without having to recreate the source file. For example you are working with LINQ to SQL and create a DBML file. Now when you drag and drop a table it creates a partial class in designer.cs and all table columns have properties in the class. You need more columns in this table to bind on the UI grid but you don't want to add a new column to the database table so you can create a separate source file for this class that has a new property for that column and it will be a partial class. So that does affect the mapping between database table and DBML entity but you can easily get an extra field. It means you can write the code on your own without messing with the system generated code.
多个开发人员可以同时为类编写代码。
压缩大型类可以更好地维护应用程序。假设您有一个具有多个接口的类,因此您可以根据接口实现创建多个源文件。理解和维护源文件具有部分类的接口是很容易的。
推荐文章
- 为什么我不能在c#中有抽象静态方法?
- net HttpClient。如何POST字符串值?
- Symfony 2.x中的所有东西都应该捆绑吗?
- 我如何使一个方法的返回类型泛型?
- 何时处理CancellationTokenSource?
- 如何获取正在执行的程序集版本?
- AutoMapper vs valueinjector
- 为什么控制台不。Writeline,控制台。在Visual Studio Express中编写工作?
- 什么是.NET程序集?
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作