部分元素与主要的 OO 编程概念相比如何?它们是主要的还是后备的?
对我来说,部分类感觉就像继承。有时我发现自己想知道我应该导出或偏化或进行扩展的天气。所有这三个都扩展了类型的行为。
帮助我在等效机制之间做出决定的一些事情是:
它利用了 OO 编程的哪些元素?
它被视为首选还是后备?
我对继承等部分类的看法是否正确?它们的使用是否应该仅限于某些特殊情况?
如果有人能给我一些历史背景那就更好了——谁想出来的?他们最初解决的具体问题是否与扩展方法如何促进 LINQ 类似?
Partial classes feel like inheritance to me. Sometimes I find myself wondering weather I should derive or partialize or make an extension. All three extend the behavior of a type.
Some things that helps me decide between equivalent mechanisms are:
What element of OO programming does it leverage?
Is it considered a first choice or a fall back?
Am I correct in viewing partial classes like inheritance and should their use be limited to certain special cases?
A bonus would be if someone could give me some historical background - who thought them up? was their a specfic problem that they originally solved, comparable to how extension methods facilitated LINQ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
部分类只是一种组织方法。它们与继承没有任何关系,因为一旦编译,它们就会完全“消失”。
这只是将代码拆分到多个文件之间的一种方法。
话虽这么说,我个人只建议使用分部类来扩展由工具(例如 UI 设计器、服务契约、ORM 等)生成的类。在您自己的代码中使用分部类往往会导致类它们太大了,应该重构为单独的、更小的类,每个类都有一个目的。
Partial classes are just an organizational method. They are in no way related to inheritance, as, once compiled, they "disappear" entirely.
It's just a way to split your code up between multiple files.
That being said, I personally would only recommend using partial classes to extend a class that's being generated by a tool, such as a UI designer, a service contract, an ORM, etc. Using partial classes for your own code tends to lead to classes that are too large, and which should be refactored into separate, smaller classes that each serve one purpose.
部分是编译器功能,与 OO 无关。
在 C# 中,它们只是允许您将一个类拆分为多个单独的文件。
这是一种允许生成的代码与手工编写的代码一起使用而不会影响另一个的方法(我知道这是该功能的简化)。
我建议阅读有关该功能的信息。
Partials are a compiler feature, nothing to do with OO.
In C# they simply allow you to split a class across several separate files.
It is a way to allow generated code to live with hand crafted code without one effecting the other (a simplification of the feature, I know).
I suggest reading about the feature.
部分类的一个问题是它们使继承变得复杂。如果您想让您的类从另一个类继承,则必须将所有类声明(每个部分和公共)更改为继承。
我建议永远不要在 WinForms 之外使用它们。如果您需要扩展一个类而不继承,请使用扩展方法,它们更干净。
One problem with partial classes is that they complicate inheritance. If you want to have your class inherit from another class, you must change all class declarations (each partial and the public) to inherit.
I would advise never using them outside of WinForms. If you need to extend a class without inheriting, use extension methods, they are much cleaner.
它与 OO 或任何其他范式无关。
这是一个后备方案,引入它是为了支持工具和设计器。
例如
MyForm.Designer.cs
、MyDataset.Designer.cs
等。如果代码全部是您的(没有工具或生成器),您就不会想使用部分代码班级。
It is not related to OO or to any other paradigm.
And it is a fallback, it was introduced to support Tools and Designers.
For example
MyForm.Designer.cs
,MyDataset.Designer.cs
etc.If the code is all yours (no tools or generators) you wouldn't want to use a partial class.
部分类型不是继承,简单明了。
部分类型只是将单个类型的代码分离为多个源文件的一种方法 - 通常这样一些可以由设计者生成,而另一些则不能。没有什么比这更多或更少的了。
他们解决的具体问题是设计器代码和手动代码之间的分离 - 避免“不要碰文件的这一部分!” .NET 1.1 中 WinForms 代码存在的问题。
请注意,.NET 2+ 和 WPF 中的 WinForms 的模型略有不同 - 在 WPF/Silverlight 中,甚至不存在可供签入的“额外源文件” - 它是在构建过程中从 XAML 生成的,就在之前类型本身是编译的。 (您仍然可以在
obj
输出目录中找到生成的代码,通常名为SomeType.g.cs
。)Partial types aren't inheritance, plain and simple.
Partial types are simply a way of separating the code for a single type into multiple source files - typically so that some can be designer-generated and others not. Nothing more or less than that.
The specific problem they solved was that separation between designer code and manual code - avoiding the "do not touch this bit of the file!" problem which existed in .NET 1.1 with WinForms code.
Note that WinForms in .NET 2+ and WPF have slightly different models - in WPF/Silverlight, the "extra source file" isn't even present to be checked in - it's generated from the XAML as part of the build process, just before the type itself is compiled. (You can still find the generated code, typically with called
SomeType.g.cs
, in theobj
output directory.)