edison0621

扩展方法原理及使用

1、写在前面
  • 什么是扩展方法
  • 扩展方法实现及其原理
  • 扩展方法的使用及其注意事项
 
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();
        }
    }
}
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();
        }
    }
}

通过以上示例,我们可以总结如下特点

接下来,我们看下反编译后的效果,
.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导入

以上为本篇文章的主要内容,希望大家多提提意见,如果喜欢记得点个赞哦

posted on 2019-03-05 17:31 艾心❤ 阅读(...) 评论(...) 编辑 收藏

相关文章: