什么是类的扩展方法

扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。

MSDN

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

传统的模式下如果想为一个类型(class)添加一个额外的自定义的特殊的逻辑上,或者业务上的新方法时,你必须重新定义的一个类型来继承原有的的方法,用继承类或者接口,但是有些用sealed修饰的,这时候就无法被继承,例如String,值类型,sealed修饰的类。

扩展方法是C#3.0这个版本提出来的。解决了必须由继承才能扩展的某个类的弊端,最重要的一点就是很好用。

平时在我们使用的过程中也经常的见到,如
这其中的OrderBy方法就是扩展方法。
MSDN

注意:

扩展方法必须在非嵌套、非泛型的静态类中定义。

Note that it is defined inside a non-nested<非嵌套>, non-generic<非泛型的> static<静态> class:

扩展方法的规则有以下几点:

  • 扩展方法必须是扩展方法必须是非嵌套、非泛型的静态类中定义的;
  • 扩展方法的第一个参数要用this关键字修饰;
  • 第一个方法参数不能有ref 或则out关键字修饰的参数;
扩展方法的调用有两个步骤
  1. 引用项目的命名空间;
  2. 参数调用两种;
  • 和传统的调用方法一样使用<ExtendClass>.<ExtendClassMethod>(参数,参数+?)
  • <参数类型>.<ExtendClassMethod>(参数+?)
高级点的应用
    在编译时绑定扩展方法<MSDN>
    你可以使用扩展方法来扩展一个类和接口,而不需要去重写他们
 // Define an interface named IMyInterface.
namespace DefineIMyInterface
{
using System; publicinterface IMyInterface
{
// Any class that implements IMyInterface must define a method
// that matches the following signature.
void MethodB();
}
} // Define extension methods for IMyInterface.
namespace Extensions
{
using System;
using DefineIMyInterface; // The following extension methods can be accessed by instances of any
// class that implements IMyInterface.
publicstaticclass Extension
{
publicstaticvoid MethodA(this IMyInterface myInterface, int i)
{
Console.WriteLine
("Extension.MethodA(this IMyInterface myInterface, int i)");
} publicstaticvoid MethodA(this IMyInterface myInterface, string s)
{
Console.WriteLine
("Extension.MethodA(this IMyInterface myInterface, string s)");
} // This method is never called in ExtensionMethodsDemo1, because each
// of the three classes A, B, and C implements a method named MethodB
// that has a matching signature.
publicstaticvoid MethodB(this IMyInterface myInterface)
{
Console.WriteLine
("Extension.MethodB(this IMyInterface myInterface)");
}
}
} // Define three classes that implement IMyInterface, and then use them to test
// the extension methods.
namespace ExtensionMethodsDemo1
{
using System;
using Extensions;
using DefineIMyInterface; class A : IMyInterface
{
publicvoid MethodB() { Console.WriteLine("A.MethodB()"); }
} class B : IMyInterface
{
publicvoid MethodB() { Console.WriteLine("B.MethodB()"); }
publicvoid MethodA(int i) { Console.WriteLine("B.MethodA(int i)"); }
} class C : IMyInterface
{
publicvoid MethodB() { Console.WriteLine("C.MethodB()"); }
publicvoid MethodA(object obj)
{
Console.WriteLine("C.MethodA(object obj)");
}
} class ExtMethodDemo
{
staticvoid Main(string[] args)
{
// Declare an instance of class A, class B, and class C.
A a = new A();
B b = new B();
C c = new C(); // For a, b, and c, call the following methods:// -- MethodA with an int argument// -- MethodA with a string argument// -- MethodB with no argument.// A contains no MethodA, so each call to MethodA resolves to // the extension method that has a matching signature.
a.MethodA(); // Extension.MethodA(object, int)
a.MethodA("hello"); // Extension.MethodA(object, string)// A has a method that matches the signature of the following call// to MethodB.
a.MethodB(); // A.MethodB()// B has methods that match the signatures of the following// method calls.
b.MethodA(); // B.MethodA(int)
b.MethodB(); // B.MethodB()// B has no matching method for the following call, but // class Extension does.
b.MethodA("hello"); // Extension.MethodA(object, string)// C contains an instance method that matches each of the following// method calls.
c.MethodA(); // C.MethodA(object)
c.MethodA("hello"); // C.MethodA(object)
c.MethodB(); // C.MethodB()
}
}
}
/* Output:
Extension.MethodA(this IMyInterface myInterface, int i)
Extension.MethodA(this IMyInterface myInterface, string s)
A.MethodB()
B.MethodA(int i)
B.MethodB()
Extension.MethodA(this IMyInterface myInterface, string s)
C.MethodA(object obj)
C.MethodA(object obj)
C.MethodB()
*/
编译器是如何发现扩展方法的
c# 3.0编译器看到某个类的调用用法时,先是从该类的实例方法中进行查找,如果没有找到与调用方法同名并参数一致的实例方法,再从所有导入的命名空间中查找是否有合适的扩展方法,并将变量类型匹配到扩展类型。
这只是我们的理解是这样一个过程,如果在同一个命名空间下,编译器则会直接用当前命名空间下符合条件的对应方法,让我看下下ILDasm.exe.
如果不是同一个程序集的命名空间可以在MANIFEST清单文件里看出。

总结的结果

方法的调用次序 类型的实例方法--->当前命名空间下的扩展方法--->导入的其他命名空间扩展方法。

引发疑问

在空引用上调用实例方法或静态方法时会抛出NullReferenceException异常?那么在类调用扩展方法时会出现异常吗?(不使用类中的一些属性或方法,强调调用)

        public static void NullUse(this TestExtend sextend)
        {
            Console.WriteLine("NUll used Method");
        }
分析:在空的类型上定义扩展方法不出现NullReferenceException,只是把空这个引用当成参数传入静态方法,
扩展方法 静态方法
TestExtend sExtend=null;

            sExtend.NullUse();
TestExtend sExtend=null;
NullUse.(sExtend);
 
 
 
从ILDASM能看出我们猜想的正确性
Notice

可能引发的子类污染的问题例如

        public static bool isNull(this object str)  
        {
            return str == null;
        }

你本来只想扩展TestExtend 这个类的方法,由于你要使用iSNull这个类型的扩展结果传入参数(object),结果导致所有的object 类型都扩展了这个方法,这会带来可怕的后果。

所以在扩展一个类型的方法时,要从确定类型扩展。尽量避免从父类去扩展。

有什么不对的或者错误的地方希望大家给予指正,谢谢

我的开发环境VS2015

DEMO的下载  【http://pan.baidu.com/s/1bY51P8】

C#3.0扩展方法学习篇的更多相关文章

  1. 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇

    最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...

  2. C#3.0 扩展方法

    扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用.对于用 C# 和 Visual ...

  3. C# 3.0 扩展方法[转载]

    实践 扩展方法是C# 3.0中新加入的特性.MSDN中对扩展方法的定义是:扩展方法使您能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 以下以 ...

  4. C#扩展方法学习

    扩展方法的本质是什么,详细见此文 C#扩展方法,爱你在心口难开 重点如下:扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法 ...

  5. C#扩展方法学习笔记

    C#扩展方法,简单的理解是不修改原来类的源代码的情况下,为某个类添加某个方法.扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的.它们的第一个参数指定该方法作用于哪个类型,并且该参数以 th ...

  6. C# 3.0 扩展方法&接口

    namespace ExtensionInterfaceMethod { class Program { static void Main(string[] args) { //使用接口变量来调用扩展 ...

  7. C#的扩展方法学习

    一,什么是扩展方法? 1,扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 2,扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用 ...

  8. C#3.0新特性之扩展方法介绍

    C#3.0扩展方法是给现有类型添加一个方法.现在类型即可是基本数据类型(如int,String等),也可以是自己定义的类.以下是引用片段: //Demo--1 //扩展基本类型 namespace T ...

  9. [读书笔记]C#学习笔记五: C#3.0自动属性,匿名属性及扩展方法

    前言 这一章算是看这本书最大的收获了, Lambda表达式让人用着屡试不爽, C#3.0可谓颠覆了我们的代码编写风格. 因为Lambda所需篇幅挺大, 所以先总结C#3.0智能编译器给我们带来的诸多好 ...

随机推荐

  1. CSS 清除默认样式

    通常有以下几句就够了: *{margin:0;padding:0} li{list-style:none} img{vertical-align:top;border:none} 如果你想写全也可以: ...

  2. BMW

    tc金游世界单登陆允许服务 tc金游世界注册机允许服务 tc金游辅助智能游戏清分允许服务[智能游戏] tc金游辅助挂机王清分允许服务[游辅助挂机清分] tc金游世界王自动打牌允许服务[自动打牌] tc ...

  3. python enumerate用法

    含义:"枚举,列举" 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值 enumerate多 ...

  4. SpringMVC中如何在网站启动、结束时执行代码(详细,确保可用)

        在一个网站启动.结束时,我们经常有些操作是需要执行的. 熟悉Asp.net的朋友,使用Global.asax很容易就搞定,在其中有Application_Start和Application_E ...

  5. Python +crontab定时备份目录发送邮件

    公司有一台静态页面展示服务器仅供给客户展示我们做的项目,当时买的时候是最低配,也就是磁盘空间为20G的系统盘,考虑到代码量很小所以没有另加磁盘,后来项目多了,就写了个crontab 定时备份目录. 就 ...

  6. wkhtmltopdf 将网页转换为PDF和图片

    wkhtmltopdf 是一个shell工具,它使用了WebKit渲染引擎和Qt,将网页html转换为pdf的强大工具,转换后的pdf也可以通过pdf工具进行复制.备注.修改 官网下载地址:http: ...

  7. SQL语句实现Split并合并查询结果

    需求是这样的,需要将数据库中的支付方式列(用";"分割的字符串)按支付方式拆分: 首先参考博客园split的文章,我采用方法2, IF EXISTS ( SELECT * FROM ...

  8. [UWP]UWP App Data存储和获取

    这篇博客介绍如何在UWP开发时,如何存储App Data和获取. App Data是指用户的一些设定,偏好等.例如,App的主题,是否接收推送,离线接收消息等.需要区分下App Data和User D ...

  9. 进击的Python【第二章】:Python基础(二)

    Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers ...

  10. VS2012调试时很慢的解决方案

      1.转自http://guooge.com/archives/408.html VS2010调试极慢获取出现死机,因为启动了IntelliTrace Visual Studio 2010 Ulti ...