什么时候适合使用C#部分类? [英] When is it appropriate to use C# partial classes?

查看:93
本文介绍了什么时候适合使用C#部分类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以概述我为什么要使用它们以及在此过程中将获得什么好处.

I was wondering if someone could give me an overview of why I would use them and what advantage I would gain in the process.

推荐答案

部分类的最大用途是使代码生成器/设计人员的工作更加轻松.局部类使生成器可以简单地发出它们需要发出的代码,而不必处理用户对该文件的编辑.用户同样可以通过拥有第二部分类来自由地用新成员为类添加注释.这为分离关注点提供了一个非常干净的框架.

The biggest use of partial classes is to make life easier for code generators / designers. Partial classes allow the generator to simply emit the code they need to emit and they do not have to deal with user edits to the file. Users are likewise free to annotate the class with new members by having a second partial class. This provides a very clean framework for separation of concerns.

一种更好的观察方法是在局部类之前了解设计器的功能. WinForms设计器会在区域内吐出所有代码,并带有措辞强烈的注释,说明不要修改代码.它必须插入各种试探法以找到生成的代码,以供以后处理.现在,它可以简单地打开designer.cs文件,并且高度确信它仅包含与设计者相关的代码.

A better way to look at it is to see how designers functioned before partial classes. The WinForms designer would spit out all of the code inside of a region with strongly worded comments about not modifying the code. It had to insert all sorts of heuristics to find the generated code for later processing. Now it can simply open the designer.cs file and have a high degree of confidence that it contains only code relevant to the designer.

这篇关于什么时候适合使用C#部分类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆