C# 3.0 中如何使用部分方法?
我已阅读最新的 C# 语言规范 中的部分方法,因此我了解原理,但我想知道人们实际上是如何使用它们的。 是否存在受益于部分方法的特定设计模式?
I have read about partial methods in the latest C# language specification, so I understand the principles, but I'm wondering how people are actually using them. Is there a particular design pattern that benefits from partial methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
引入分部方法的原因与 .Net 2 中引入分部类的原因类似。
分部类是可以拆分为多个文件的类 - 编译器在运行时将它们全部构建到一个文件中。
这样做的优点是 Visual Studio 可以为类的一部分提供图形设计器,而编码人员则可以处理其他部分。
最常见的例子是表单设计器。 开发人员不想在大多数时间手动定位按钮、输入框等。
#region
块中自动生成的代码。这使得维护两者变得更加容易。 合并更简单,VS 表单设计者意外撤消编码人员手动更改的风险也更小。
在.Net 3.5中引入了Linq。 Linq 有一个 DBML 设计器,用于构建数据结构,并生成自动代码。
这里额外的一点是需要提供开发人员可能想要填写的方法的代码。
由于开发人员将扩展这些类(使用额外的部分文件),因此他们不能在这里使用抽象方法。
另一个问题是,大多数时候这些方法不会被调用,调用空方法是浪费时间。
空方法不会被优化。
因此 Linq 生成空的部分方法。 如果您不创建自己的部分来完成它们,C# 编译器只会优化它们。
这样它就可以做到这个部分方法总是返回 void。
如果您创建一个新的 Linq DBML 文件,它将自动生成一个部分类,例如
然后在您自己的部分文件中您可以扩展它:
如果您不扩展这些方法,它们就会立即得到优化。
部分方法不能是公共的 - 因为这样它们就必须存在以供其他类调用。 如果你编写自己的代码生成器,我可以看到它们很有用,但除此之外它们只对 VS 设计者真正有用。
我之前提到的示例是一种可能性:
另一种潜在用途是,如果您有一个大型且复杂的类分布在多个文件中,您可能需要在调用文件中进行部分引用。 不过我认为在这种情况下你应该首先考虑简化课程。
Partial methods have been introduced for similar reasons to why partial classes were in .Net 2.
A partial class is one that can be split across multiple files - the compiler builds them all into one file as it runs.
The advantage for this is that Visual Studio can provide a graphical designer for part of the class while coders work on the other.
The most common example is the Form designer. Developers don't want to be positioning buttons, input boxes, etc by hand most of the time.
#region
blockThis makes maintaining both much easier. Merges are simpler and there's less risk of the VS form designer accidentally undoing coders' manual changes.
In .Net 3.5 Linq has been introduced. Linq has a DBML designer for building your data structures, and that generates auto-code.
The extra bit here is that code needed to provide methods that developers might want to fill in.
As developers will extend these classes (with extra partial files) they couldn't use abstract methods here.
The other issue is that most of the time these methods wont be called, and calling empty methods is a waste of time.
Empty methods are not optimised out.
So Linq generates empty partial methods. If you don't create your own partial to complete them the C# compiler will just optimise them out.
So that it can do this partial methods always return void.
If you create a new Linq DBML file it will auto-generate a partial class, something like
Then in your own partial file you can extend this:
If you don't extend these methods they get optimised right out.
Partial methods can't be public - as then they'd have to be there for other classes to call. If you write your own code generators I can see them being useful, but otherwise they're only really useful for the VS designer.
The example I mentioned before is one possibility:
Another potential use is if you had a large and complex class spilt across multiple files you might want partial references in the calling file. However I think in that case you should consider simplifying the class first.
以下是 C#.NET 3.0 中部分类的最佳资源: http://msdn.microsoft.com/en-us/library/wa80x488(VS.85).aspx
我尝试避免使用部分类(由 Visual Studio 为设计器文件创建的部分类除外;那些都很棒)。 对我来说,将一个类的所有代码放在一个地方更重要。 如果您的类设计良好并且代表一件事(单一职责原则),那么所有某件事的代码应该放在一个地方。
Here is the best resource for partial classes in C#.NET 3.0: http://msdn.microsoft.com/en-us/library/wa80x488(VS.85).aspx
I try to avoid using partial classes (with the exception of partials created by Visual Studio for designer files; those are great). To me, it's more important to have all of the code for a class in one place. If your class is well designed and represents one thing (single responsibility principle), then all of the code for that one thing should be in one place.
代码生成是主要原因之一它们的存在也是使用它们的主要原因之一。
编辑:尽管该链接指向特定于 Visual Basic 的信息,但相同的基本原理也与 C# 相关。
Code generation is one of main reasons they exist and one of the main reasons to use them.
EDIT: Even though that link is to information specific to Visual Basic, the same basic principles are relevant to C#.
我认为它们是轻量级的活动。 您可以拥有一个可重用的代码文件(通常是自动生成的,但不一定),并且对于每个实现,只需处理您在分部类中关心的事件。 事实上,这就是它在 LINQ to SQL 中的使用方式(也是发明该语言功能的原因)。
I see them as lightweight events. You can have a reusable code file (usually autogenerated but not necessarily) and for each implementation, just handle the events you care about in your partial class. In fact, this is how it's used in LINQ to SQL (and why the language feature was invented).
部分方法在概念上与 GoF 模板方法 行为模式非常相似 (设计模式< /a>,第 325 页)。
它们允许在一个地方定义算法或操作的行为,并在其他地方实现或更改,从而实现可扩展性和定制。 我开始在 C# 3.0 中使用部分方法而不是模板方法,因为我认为代码更干净。
一个很好的功能是,未实现的部分方法在编译时不会产生运行时开销。
Partial methods are very similar in concept to the GoF Template Method behavioural pattern (Design Patterns, p325).
They allow the behaviour of an algorithm or operation to be defined in one place and implemented or changed elsewhere enabling extensibility and customisation. I've started to use partial methods in C# 3.0 instead of template methods because the I think the code is cleaner.
One nice feature is that unimplemented partial methods incur no runtime overhead as they're compiled away.