对象的C#扩展方法 [英] C# Extension Method for Object
本文介绍了对象的C#扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Object类上使用扩展方法是一个好主意吗?
Is it a good idea to use an extension method on the Object class?
我想知道是否通过注册此方法是否会导致性能下降,因为它会加载到上下文中加载的每个对象上.
I was wondering if by registering this method if you were incurring a performance penalty as it would be loaded on every object that was loaded in the context.
推荐答案
除了其他答案:
因为扩展方法是编译器功能,所以不会降低性能.考虑以下代码:
there would be no performance penalty because extension methods is compiler feature. Consider following code:
public static class MyExtensions
{
public static void MyMethod(this object) { ... }
}
object obj = new object();
obj.MyMethod();
对MyMethod
的调用实际上将编译为:
The call to MyMethod
will be actually compiled to:
MyExtensions.MyMethod(obj);
这篇关于对象的C#扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文