扩展方法原理及使用
1、写在前面
今天群里一个小伙伴问了这样一个问题,扩展方法与实例方法的执行顺序是什么样子的,谁先谁后。扩展方法在我的项目经历中用的很多,可以说的上来,但是里面的原理,却并没有那么清晰,所以写了这边文章,力图从原理角度解释扩展方法及其使用。
以下为主要内容:
-
什么是扩展方法
-
扩展方法实现及其原理
-
扩展方法的使用及其注意事项
2、什么是扩展方法
一般而言,扩展方法为现有类型添加新的方法(从面向对象的角度来说,是为现有对象添加新的行为)而无需修改原有类型,这是一种无侵入而且非常安全的方式,同时扩展方法无法访问扩展类中的任何隐私数据。扩展方法是静态的,它的使用和其他实例方法几乎没有什么区别。常见的扩展方法有Linq扩展、有IEnumerable扩展等。
先让我们来感受一下,扩展方法:
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleLab { class Program { static void Main(string[] args) { List<int> lst = new List<int> { 1, 2, 3, 4 }; lst.OrderBy(p => p); Console.WriteLine(lst.Aggregate(string.Empty, (next, str) => next += str + ",")); Console.Read(); } } }
输出:
以上用到了List扩展里面的OrderBy和Aggregate,不得不说,.NET在这方面做得非常精致。可是话说回来,它是如何实现的呢。接下来我们重点关注一下
3、扩展方法实现及其原理
我们先看一个关于自定义扩展方法的范例
using ConsoleExtension; using System; namespace ConsoleExtension { internal static class StringExtension { internal static int ToInt(this string str) { if (int.TryParse(str, out int result)) { return result; } throw new ArgumentException("无效参数"); } } } namespace ConsoleLab { class Program { static void Main(string[] args) { Console.WriteLine("2".ToInt()); Console.Read(); } } }
通过以上示例,我们可以总结如下特点
- 定义的扩展方法必须包括在静态类中,其本身也是静态的
- 改静态的可见性至少与所在类的可见性相同,也就是同时public或者internal
- 此方法的第一个参数指定方法所操作的类型;此参数前面必须加上 this 修饰符
- 在调用代码中,添加 using 指令,用于指定包含扩展方法类的命名空间
- 调用方式和调用实例方法一样
接下来,我们看下反编译后的效果,
.class public auto ansi abstract sealed beforefieldinit ConsoleLab.StringExtension extends [mscorlib]System.Object { .custom instance void [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 ) // Methods .method public hidebysig static int32 ToInt ( string str ) cil managed { .custom instance void [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute
::.ctor() = ( 01 00 00 00 ) // Method begins at RVA 0x2050 // Code size 31 (0x1f) .maxstack 2 .locals init ( [0] int32, [1] bool, [2] int32 ) IL_0000: nop IL_0001: ldarg.0 IL_0002: ldloca.s 0 IL_0004: call bool [mscorlib]System.Int32::TryParse(string, int32&) IL_0009: stloc.1 IL_000a: ldloc.1 IL_000b: brfalse.s IL_0012 IL_000d: nop IL_000e: ldloc.0 IL_000f: stloc.2 IL_0010: br.s IL_001d IL_0012: ldstr "无效参数" IL_0017: newobj instance void [mscorlib]System.ArgumentException::.ctor(string) IL_001c: throw IL_001d: ldloc.2 IL_001e: ret } // end of method StringExtension::ToInt } // end of class ConsoleLab.StringExtension
以上是StringExtension.ToInt()后的效果,和普通方法其实也没有什么区别
.class private auto ansi beforefieldinit ConsoleLab.Program extends [mscorlib]System.Object { // Methods .method private hidebysig static void Main ( string[] args ) cil managed { // Method begins at RVA 0x207b // Code size 24 (0x18) .maxstack 8 .entrypoint IL_0000: nop IL_0001: ldstr "2" IL_0006: call int32 ConsoleLab.StringExtension::ToInt(string) IL_000b: call void [mscorlib]System.Console::WriteLine(int32) IL_0010: nop IL_0011: call int32 [mscorlib]System.Console::Read() IL_0016: pop IL_0017: ret } // end of method Program::Main .method public hidebysig specialname rtspecialname instance void .ctor () cil managed { // Method begins at RVA 0x2094 // Code size 8 (0x8) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: nop IL_0007: ret } // end of method Program::.ctor } // end of class ConsoleLab.Program
以上是Program类反编译后的源码,其调用方式跟调用静态方法没有什么区别StringExtension::ToInt
到了这个地方,我们可以看到,编译后的代码调用其实和调用一个类的静态方法一样。只不过是.NET标记了这个方法为扩展方法,也就是我们看到的System.Runtime.CompilerServices.ExtensionAttribute
写到最后
扩展方法虽然看起来挺不错的,但是我们也要谨慎的使用,因为扩展的源对象如果发生了变化,就会导致bug的出现如果确实为给定类型实现了扩展方法,请记住以下几点:
-
如果扩展方法与其实例方法具有相同的签名,扩展方法会被实例方法覆盖掉,从而无法调用扩展方法,这样也避免了扩展方法对原有代码所带来的损害,这就回答了,文章开头所提出的问题
-
如果扩展方法和调用方不在同一个命名空的,需要使用
using导入
以上为本篇文章的主要内容,希望大家多提提意见,如果喜欢记得点个赞哦