如何扩展方法下,引擎罩的工作? [英] How do extension methods work under-the-hood?

查看:98
本文介绍了如何扩展方法下,引擎罩的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我使用扩展方法工作的承包商实施 CRUD 著名的内部类我们自己的。我说这是更好地使用正常继承扩展方法由于以下原因。




  • 使用扩展方法混淆,隐藏和放大器;混淆了 CRUD 方法的来源。

  • 我假设扩展方法制作大量使用反射(这是慢)。



他的逻辑是, 这是编译,所以它的速度快。也许我错了......但只是因为它编译并不意味着它不使用反射,也不意味着它比正常的快继承



所以我的问题是:




  1. 如何扩展方法下,任何─工作油烟机?

  2. 是它更好地使用继承扩展方法上WELL -KNOWN类,你自己?


解决方案

如何延长?方法下引擎罩工作




他们只是静态方法;编译器重写像 myObject.MyExtensionMethod电话() MyExtensionClass.MyExtensionMethod(myObject的)




是更好的使用inheretance或扩展方法您拥有驰名类?




这里没有单一的回答这个问题,这一切都取决于上下文。但通常的扩展方法是在这种情况下最有用的:




  • 您不拥有扩展类型代码

  • 方法针对的界面,将成为该接口的所有实现相同的(例如的IEnumerable< T> 和LINQ扩展方法)


A contractor where I work is using extension methods to implement CRUD on well-known internal classes that we own. I say it is better to use normal inheritance over extension methods for the following reasons.

  • Using extension methods obfuscates, hides & confuses the source of the CRUD methods.
  • I assume extension methods make heavy use of reflection (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:

  1. How do extension methods work under-the-hood?
  2. Is it better to use inheritance or extension 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() to MyExtensionClass.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆