C#扩展方法

扩展方法的特点:

1、扩展方法必须写在静态类静态方法中;

namespace ExtensionMethods
{
    public static class Extensions
    {
        public static void MethodA(this String str)
        {
            Console.WriteLine(str);
        }
    }   
}

2、静态方法的第一个参数指定该方法作用于哪个类型,并且该参数以this修饰符为前缀;

namespace ExtensionMethods
{
    public static class Extensions
    {
        public static String MethodB(this String str, String str1)
        {
         	return str + str1;
        }
    }   
}

3、必须使用using指令将命名空间导扩到源代码之后,扩展方法才位于范围中;

4、在代码中,使用实例方法语法调用扩展方法;

namespace ExtensionMethodsdemo
{
	using System;
	using Extensions;

	class ExtMethodDemo
	{
		static void Main(string[] args)
		{
            		String tmp = "hello world!";
			tmp.MethodA();
			Console.WriteLine(tmp.MethodB("I'm here."));
                        Console.ReadKey();
		}
	}
}

输出结果:

与接口或类方法具有相同名称和签名的扩展方法永远不会被调用。编译时,扩展方法的优先级总是比类型本身中定义的实例方法低。当编译器遇到方法调用时,它首先在该类型的实例方法国寻找匹配的方法,如果未找到任何匹配方法,编译器将搜索为该类型定义的任何扩展方法,并且绑定到它找到的第一个扩展方法。

建一个新项目,定义一个动物类型

using System;

namespace AnimalLibrary
{
    public class Animal
    {
        public string Name { get; set; }

        public void Cry()
        {
            Console.WriteLine(Name + "在叫");
        }
    }
}
建立扩展项目
using AnimalLibrary;
using System;

namespace ExtensionLibrary
{
    public static class Extensions
    {
        public static void Cry(this Animal animal)
        {
            Console.WriteLine(animal.Name + "又在叫了");
        }

        public static void Cry(this Animal animal, String cause)
        {
            Console.WriteLine("{0},{1}一直在叫", cause, animal.Name);
        }
    }
}
测试项目
using System;
using ExtensionLibrary;
using AnimalLibrary;

namespace ExtensionMethodTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Animal cat = new Animal { Name = "猫" };
            cat.Cry();
            cat.Cry("有老鼠");
            Console.ReadKey();
        }
    }
}
运行结果


扩展方法可以实现重构。

扩展方法无法访问它们所扩展的类型的私有变量。

编译器生成的中间语言(IL)会将代码转换为对静态方法的调用,因此,并未真正违反封装原则。


在使用扩展方法来扩展您无法更改其源代码的类型时,您需要承受该类型实现的更改会导致扩展方法失效的风险。
如果执行特定类型的扩展方法,请确保:
1、如果扩展方法与该类型中定义的方法具有相同的签名,则扩展方法永远不会被调用。
2、扩展方法被在命名空间级别放入范围中。如果您在同一个名为Extensions的命名空间中具有多个包含扩展方法的静态类,则这些扩展方法将全部由using Extensions;指令放入范围中。


参考:

MSDN

http://jingyan.baidu.com/article/4d58d541ec888c9dd5e9c075.html

http://www.cnblogs.com/suger/archive/2012/05/13/2498248.html#commentform


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值