扩展.NET Framework 3.5

Extention Methods in C# 3.0

C#3.0中的扩展方法

by Ivo Stoykov

通过伊沃·斯托伊科夫(Ivo Stoykov)

C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance.

C#3.0提供了扩展方法。 它们允许扩展现有的类,而无需更改类的源代码或依靠继承。

These are static methods invoked as instance method. This allows developers to add functionality without necessity to create derived types, re-compiling or other modifications of existing objects. Also to extend sealed objects and avoiding deep digging into object inheritance tree.

这些是作为实例方法调用的静态方法。 这允许开发人员添加功能,而无需创建派生类型,重新编译或对现有对象进行其他修改。 还可以扩展密封对象,并避免深入挖掘对象继承树。

Declaration

宣言

There are few "must do" in extension method declaration

扩展方法声明中很少有“必须做”

1. Must be declared into static class

1.必须声明为静态类

2. Method must be static

2.方法必须是静态的

3. 1st parameter must be this.

3.第一个参数必须是

This first parameter in method declaration is the most important one. It specifies witch type the method operates on. The keyword this defines that method is applied on the instance of defined type.

方法声明中的第一个参数是最重要的一个。 它指定方法操作的类型。 关键字定义了方法上定义类型的实例应用。

In the sample below we have only one parameter compound by three parts. Part one is the keyword stating that the method WordCount is applicable to String type.

在下面的示例中,我们只有一个参数化合物,分为三部分。 第一部分是关键字,说明String类型。

Part two is the type itself -- String in our sample.

第二部分是类型本身-示例中的String

Part three is the particular instance of the type the method will be executed -- in the sample this is property name s. In other words the method will be applicable to any String instance.

第三部分是将要执行的方法类型的特定实例-在示例中,这是属性名称

In the sample we define in Main method a variable of String type named str. In the next row we use standard notation -- type, dot, method, parameters -- str type on the left of the dot becomes out first parameter in WordCount method and, so str becomes s representing String instance passed as this.

在示例中,我们在Main方法中定义了一个名为String类型的变量。 在下一行,我们使用标准的符号-类型,点,方法,参数- 传递的字符串实例。

This is why extension methods might appear anywhere (same as in the sample or any other file; same or any other project) and will be available all the time if their namespace is explicitly imported into source code with a using directive.

这就是为什么扩展方法可能会出现在任何地方(与示例或任何其他文件中的文件;相同或任何其他项目)一起出现,并且如果使用using指令将其命名空间显式导入到源代码中的话,那么扩展方法将始终可用。

What Extension Method Cannot do

扩展方法无法执行的操作

* Extension methods cannot be used to override existing methods

*扩展方法不能用于覆盖现有方法

* An extension method with the same name and signature as an instance method will not be called

*与实例方法具有相同名称和签名的扩展方法将不会被调用

* The concept of extension methods cannot be applied to fields, properties or events

*扩展方法的概念不能应用于字段,属性或事件

// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ExtensionMethodTest
{
  static class StrExt
  {
    public static int WordCount(this String s)
    {
      return s.Split(new char[] {'\t', ' ', ',', '.', '?', '!', ';', ':'}, StringSplitOptions.RemoveEmptyEntries).Length;
    }
  }
 
  class Program
  {
    static void Main(string[] args)
    {
      String str = "This string! has; few words! This is, shoerter.";
 
      Console.WriteLine("string " + str + Environment.NewLine + "has " + str.WordCount() + " words");
    }
  }
}

翻译自: https://www.experts-exchange.com/articles/2105/Extending-NET-Framework-3-5.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值