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

查看:19
本文介绍了什么时候适合使用 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天全站免登陆