I'm still not really getting the开发者_运维技巧 upside of partial methods. Can anyone illustrate a problem that partial methods are ideally suited to solve?
From Partial Class and Methods (C# Programming Guide) on the MSDN:
A partial class or struct may contain a partial method. One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.
Partial methods enable the implementer of one part of a class to define a method, similar to an event. The implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method. The calls to the method, including any results that would occur from evaluation of arguments in the calls, have no effect at run time. Therefore, any code in the partial class can freely use a partial method, even if the implementation is not supplied. No compile-time or run-time errors will result if the method is called but not implemented.
Partial methods are especially useful as a way to customize generated code. They allow for a method name and signature to be reserved, so that generated code can call the method but the developer can decide whether to implement the method. Much like partial classes, partial methods enable code created by a code generator and code created by a human developer to work together without run-time costs.
In my opinion, I would recommend to avoid using these unless you have a particular, specific need for them.
Essentially, the most useful purpose of partial methods is for a code generation system to provide an API for extending the capability of its properties and methods without using inheritance.
Take a look at any Linq to SQL data model for a quick example.
The generated code includes partial methods which, if implemented in your own partial class, grant the ability to execute validation logic, event notifications, etc. within existing properties.
What makes partial methods appealing is that, if you do not implement them in your own partial class, they are not emitted into the compiled code at all, which provides a modest efficiency boost.
Here's a decent blog entry that demonstrates the use of partial methods to inject validation logic:
http://www.davidhayden.com/blog/dave/archive/2007/07/24/LINQToSQLValidationEnterpriseLibraryValidationApplicationBlock.aspx
Generated code. Plain and simple this was the number one reason they were implemented. Look at something like WPF. The UI is done declaratively in XAML and the "code-behind" is in C#. Both parts are the same class split by using the partial class concept
As I understand it, one of the main benefits is the ability to have a code-generated "stub" that you can choose whether to implement. So your code-gen creates a partial method and calls some Validate method. In order to "plug in" your validation, you just implement the partial method. The "partial" keyword allows for a relatively clean development process.
精彩评论