没有得到通过而TYPEx说,我应该定义内而TYPEx这些方法,而不是扩展)我就可以了(没有控制忘乎所以实际的类,我定义它是SPListItem。
Dont get carried away by TypeX saying that I should define those methods inside TypeX rather than the Extension) I have no control on it (Actual Class I defined it is on SPListItem.
我想转换,而TYPEx为int或布尔和这个操作是,我做的很多,在code的地方之一常见的事情。我想知道这会造成性能degrade.I试图跨preT IL code。使用反射,但我不擅长。可以是上述的例子有不会是任何性能劣化。总的来说,我想知道有关于性能的影响,同时使用扩展方法。
I wanted to convert the TypeX to Int or Boolean and this operation is one Common thing that I am doing in lots of Places in the code. I wanted to know will this cause performance degrade.I tried to interpret IL code using Reflector, but am not good at it. May be for the above example there wont be any performance degrade. In general I wanted to know about the implications with Regard to the Performance while using the Extension methods.
推荐答案扩展方法的只是的从一个编译时的变化:
Extension methods are just a compile-time change from:
x.GetValueAsBoolean()
到
Extensions.GetValueAsBoolean(x)这是所有的参与 - 翻译看起来像一个实例的方法调用将调用一个静态方法
That's all that's involved - translating what looks like an instance method call into a call to a static method.
如果您还没有与静态方法的性能问题,然后使它的扩展方法不会引入任何新的问题。
If you don't have performance problems with the static method, then making it an extension method won't introduce any new problems.
编辑:IL,根据要求...
IL, as requested...
以这个样本:
using System; public static class Extensions { public static void Dump(this string x) { Console.WriteLine(x); } } class Test { static void Extension() { "test".Dump(); } static void Normal() { Extensions.Dump("test"); } }
下面是IL为扩展和正常:
.method private hidebysig static void Extension() cil managed { // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "test" IL_0006: call void Extensions::Dump(string) IL_000b: nop IL_000c: ret } // end of method Test::Extension .method private hidebysig static void Normal() cil managed { // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "test" IL_0006: call void Extensions::Dump(string) IL_000b: nop IL_000c: ret } // end of method Test::Normal
正如你所看到的,他们是完全一样的。
As you can see, they're exactly the same.
更多推荐
扩展方法性能
发布评论