zoukankan      html  css  js  c++  java
  • [转]C#原始类型扩展方法—this参数修饰符

     扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。对于用 C# 和 Visual Basic 编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异。

     扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。仅当您使用 using 指令将命名空间显式导入到源代码中之后,扩展方法才位于范围中。

     下面的示例演示为 System.String 类定义的一个扩展方法。请注意,它是在非嵌套、非泛型静态类内部定义的:

    1 namespace ExtensionMethods
    2 {
    3 public static class MyExtensions
    4 {
    5 public static int WordCount(this String str)
    6 {
    7 return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
    8 }
    9 }
    10 }

    可使用以下 using 指令将 WordCount 扩展方法放入范围中:
      using ExtensionMethods;
      而且,可以在应用程序中使用以下语法对该扩展方法进行调用:
      string s = "Hello Extension Methods";
      int i = s.WordCount();

      在代码中,可以使用实例方法语法调用该扩展方法。但是,编译器生成的中间语言 (IL) 会将代码转换为对静态方法的调用。因此,并未真正违反封装原则。实际上,扩展方法无法访问它们所扩展的类型中的私有变量。

    定义和调用扩展方法:
     1、定义一个静态类以包含扩展方法。该类必须对客户端代码可见。有关可访问性规则的更多信息,请参见访问修饰符(C# 编程指南)。
     2、将该扩展方法实现为静态方法,并使其至少具有与包含类相同的可见性。
     3、该方法的第一个参数指定方法所操作的类型;该参数必须以 this 修饰符开头。
     4、在调用代码中,添加一条 using 指令以指定包含扩展方法类的命名空间。
     5、按照与调用类型上的实例方法一样的方式调用扩展方法。
      请注意,第一个参数不是由调用代码指定的,因为它表示正应用运算符的类型,并且编译器已经知道对象的类型。您只需通过 n 为这两个形参提供实参。

    示例
      下面的示例在 MyExtensions.StringExtension 类中实现了一个名为 WordCount 的扩展方法。该方法对 String 类进行操作,而该类被指定为第一个方法参数。MyExtensions 命名空间被导入到应用程序命名空间中,并且该方法是在 Main 方法内调用的。

    1 using System.Linq;
    2  using System.Text;
    3  using System;
    4
    5 namespace CustomExtensions
    6 {
    7 //Extension methods must be defined in a static class
    8 public static class StringExtension
    9 {
    10 // This is the extension method.
    11 // The first parameter takes the "this" modifier
    12 // and specifies the type for which the method is defined.
    13 public static int WordCount(this String str)
    14 {
    15 return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;
    16 }
    17 }
    18 }
    19 namespace Extension_Methods_Simple
    20 {
    21 //Import the extension method namespace.
    22 using CustomExtensions;
    23 class Program
    24 {
    25 static void Main(string[] args)
    26 {
    27 string s = "The quick brown fox jumped over the lazy dog.";
    28 // Call the method as if it were an
    29 // instance method on the type. Note that the first
    30 // parameter is not specified by the calling code.
    31 int i = s.WordCount();
    32 System.Console.WriteLine("Word count of s is {0}", i);
    33 }
    34 }
    35 }
  • 相关阅读:
    理解盒子模型
    Jackson 框架,轻易转换JSON
    JAVA仿百度分页
    最干净,最便捷的卸载Mysql
    Mysql 6.0安装过程(截图放不上去)
    开发JSP自定义标签
    JAVA实现文件上传
    开发过程中常用工具类
    JQUERY 简单易用的提示框插件
    什么是Bash Shell的内建(build in)命令
  • 原文地址:https://www.cnblogs.com/chinaHunk/p/1900713.html
Copyright © 2011-2022 走看看