如何扩展方法下,引擎罩的工作? [英] How do extension methods work under-the-hood?
问题描述
在这里我使用扩展方法工作的承包商
实施 CRUD
在著名的内部类我们自己的。我说这是更好地使用正常继承
在扩展方法
由于以下原因。
- 使用扩展方法混淆,隐藏和放大器;混淆了
CRUD
方法的来源。 - 我假设
扩展方法
制作大量使用反射
(这是慢)。
他的逻辑是, 这是编译,所以它的速度快。也许我错了......但只是因为它编译并不意味着它不使用反射,也不意味着它比正常的快继承
所以我的问题是:
- 如何
扩展方法
下,任何─工作油烟机? - 是它更好地使用
继承
或扩展方法
上WELL -KNOWN类,你自己?
如何延长?方法下引擎罩工作
块引用>
他们只是静态方法;编译器重写像
myObject.MyExtensionMethod电话()
到MyExtensionClass.MyExtensionMethod(myObject的)
。
是更好的使用inheretance或扩展方法您拥有驰名类?
块引用>
这里没有单一的回答这个问题,这一切都取决于上下文。但通常的扩展方法是在这种情况下最有用的:
- 您不拥有扩展类型代码
- 方法针对的界面,将成为该接口的所有实现相同的(例如
的IEnumerable< T>
和LINQ扩展方法)
A contractor where I work is using
extension methods
to implementCRUD
on well-known internal classes that we own. I say it is better to use normalinheritance
overextension methods
for the following reasons.
- Using extension methods obfuscates, hides & confuses the source of the
CRUD
methods.- I assume
extension methods
make heavy use ofreflection
(which is slower).His logic is, "It's compiled, so it's fast." Maybe I'm wrong...but just because it is compiled doesn't mean it doesn't use reflection, nor does it mean it is faster than normal inheritance.
So my questions are:
- How do
extension methods
work under-the-hood?- Is it better to use
inheritance
orextension methods
on WELL-KNOWN classes that you OWN?
解决方案How do extension methods work under-the-hood?
They're just static methods; the compiler rewrites calls like
myObject.MyExtensionMethod()
toMyExtensionClass.MyExtensionMethod(myObject)
.Is it better to use inheretance or extension methods on WELL-KNOWN classes that you OWN?
There's not single answer to this question, it all depends on the context. But usually extension methods are most useful in those cases:
- you don't own the code for the extended type
- the method targets an interface and will be the same for all implementations of this interface (e.g.
IEnumerable<T>
and Linq extension methods)这篇关于如何扩展方法下,引擎罩的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!