|
|
此文章由 atransformer 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 atransformer 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 dadumile 于 2011-4-15 11:23 发表 
为什么是两码事? 我看都是往要使用的类中加方法. 只是在不同的条件下用. 能也点化一下我吗?
我来瞎说两句。 extension method 的一大好处是,不需要把一个很复杂的class library再编译一边。
当然了 msdn 说了
“In general, we recommend that you implement extension methods sparingly and only when you have to. Whenever possible, client code that must extend an existing type should do so by creating a new type derived from the existing type. For more information, see Inheritance (C# Programming Guide).
When using an extension method to extend a type whose source code you cannot change, you run the risk that a change in the implementation of the type will cause your extension method to break.
If you do implement extension methods for a given type, remember the following two points:
An extension method will never be called if it has the same signature as a method defined in the type.
Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using Extensions; directive.” |
|