我想知道是否有人能给我一个概述,为什么我要使用它们,在这个过程中我能获得什么好处。
当前回答
服务引用是另一个例子,其中部分类用于将生成的代码与用户创建的代码分开。
您可以在更新服务引用时“扩展”服务类,而不必覆盖它们。
其他回答
的确,Partial Class用于自动代码生成,其中一个用途是维护一个可能有上千行代码的大型类文件。你永远不知道你的类最终会有1万行,你不想用不同的名字创建一个新类。
public partial class Product
{
// 50 business logic embedded in methods and properties..
}
public partial class Product
{
// another 50 business logic embedded in methods and properties..
}
//finally compile with product.class file.
另一个可能的用途是多个开发人员可以使用同一个类,因为它们存储在不同的地方。人们可能会笑,但你永远不知道有时候情况会很棘手。
Product1.cs
public partial class Product
{
//you are writing the business logic for fast moving product
}
Product2.cs
public partial class Product
{
// Another developer writing some business logic...
}
希望这是有意义的!
下面列出了部分类的一些优点。
您可以分离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.
多个开发人员可以同时为类编写代码。
压缩大型类可以更好地维护应用程序。假设您有一个具有多个接口的类,因此您可以根据接口实现创建多个源文件。理解和维护源文件具有部分类的接口是很容易的。
我看到的另一个用法是,
扩展一个关于数据访问逻辑的大型抽象类,
我有各种文件的名字Post.cs,Comment.cs,Pages.cs…
in Post.cs
public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of post..
}
in Comment.cs
public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of comment..
}
in Pages.cs
public partial class XMLDAO :BigAbstractClass
{
// CRUD methods of Pages..
}
每当我有一个类,其中包含一个具有重要大小/复杂性的嵌套类时,我将该类标记为partial,并将嵌套类放在一个单独的文件中。我使用规则命名包含嵌套类的文件:[class name]。[嵌套类名].cs。
下面的MSDN博客解释了如何使用带有嵌套类的部分类来实现可维护性:http://blogs.msdn.com/b/marcelolr/archive/2009/04/13/using-partial-classes-with-nested-classes-for-maintainability.aspx
部分类的最大用途是使代码生成器/设计人员的工作更轻松。部分类允许生成器简单地发出它们需要发出的代码,而不需要处理用户对文件的编辑。用户同样可以通过创建第二个分部类来自由地用新成员注释类。这为关注点分离提供了一个非常清晰的框架。
更好的方法是看看设计器在部分类之前是如何工作的。WinForms设计器会吐出区域内的所有代码,并给出不修改代码的强烈注释。它必须插入各种启发式方法来找到生成的代码,以便稍后进行处理。现在它可以简单地打开designer.cs文件,并有高度的信心它只包含与设计器相关的代码。
推荐文章
- 为什么我不能在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中使用属性的特定操作