c# 扩展方法学习
在看王清培前辈的.NET框架设计时,当中有提到扩展方法 .
开头的一句话是:扩展方法是让我们在不改变类原有代码的情况下动态地添加方法的方式,这给面向对象设计 模块设计带来了质的提升
很明显,扩展方法在框架设计或者平时码代码中,是能够提升我们整个架构的灵活性的
一..net自带扩展方法和自定义扩展方法
在使用linq时就能够使用到很多.net自带的扩展方法,比如where select等等
where的扩展方法定义
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
Select的扩展方法定义
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
(1)自己实现where和select的扩展方法
// where自实现 public static IEnumerable<TSource> ExtenSionWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { if (source == null) { throw new Exception(nameof(source)); } if (predicate == null) { throw new Exception(nameof(predicate)); } List<TSource> satisfySource = new List<TSource>(); foreach (var sou in source) { if (predicate(sou)) { satisfySource.Add(sou); } } return satisfySource; } // select 自实现 public static IEnumerable<TResult> ExtenSionSelect<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector) { if(source==null) { throw new Exception(nameof(source)); } if(selector==null) { throw new Exception(nameof(source)); } List<TResult> resultList = new List<TResult>(); foreach(var sou in source) { resultList.Add(selector(sou)); } return resultList; }
(2)自实现where和select调用
static void Main(string[] args) { List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 }; //常规写法 var selectList = list.ExtenSionWhere(p => p > 3).ExtenSionSelect(p => p.ToString()).ToList(); //自定义泛型委托写法 Func<int, bool> whereFunc = (num) => num > 3; Func<int, string> selectFunc = (num) => num.ToString(); var selectList1 = list.ExtenSionWhere(p => whereFunc(p)).ExtenSionSelect(p => selectFunc(p)).ToList(); }
二.使用扩展方法实现链式编程
我在项目中经常使用开源的Flurl进行http请求,在进行拼装请求报文时,就会使用到链式编程
如下代码所示
以上代码就是使用了扩展方法进行链式编程,从而使得整个请求信息可以在一句代码中体现出来
接下来,我们自己实现链式代码
public static class ContextExtension { public static RectangleContext SetLength(this RectangleContext context,int num) { RectangleContext.Config.Length = num; return context; } public static RectangleContext SetWideth(this RectangleContext context, int num) { RectangleContext.Config.Wideth = num; return context; } public static RectangleContext SetHeight(this RectangleContext context, int num) { RectangleContext.Config.Height = num; return context; } } public class RectangleContext { public static RectangleContext Config=new RectangleContext(); public int Length { get; set; } public int Wideth { get; set; } public int Height { get; set; } }
调用和执行结果
总结
1.使用扩展方法能在不修改原有类型的基础下,动态添加方法,这使得整个框架更具有灵活性
2.在使用上下文信息的时候,可以使用链式编程,使得调用时能够在一句代码中完成所有属性设置
3.扩展方法不能滥用.添加扩展方法应当使用最小影响原则,即尽量不要在父类使用扩展方法,比如object,这将影响性能
- C# 3.0 新特性 学习(二):匿名类型、扩展方法
- C#基础:通过一个实例来学习IFormattable接口,扩展方法
- C# 扩展方法[学习]
- C#扩展方法学习
- C#基础:通过一个实例来学习IFormattable接口,扩展方法
- [读书笔记]C#学习笔记五: C#3.0自动属性,匿名属性及扩展方法
- C#学习笔记-索引器,指针类型,隐式类型,扩展方法,分部方法,匿名类型
- [C#]C#学习笔记-索引器,指针类型,隐式类型,扩展方法,分部方法,匿名类型
- C#中的扩展方法学习总结
- C#中的扩展方法学习总结
- C# 扩展方法[学习]
- [读书笔记]C#学习笔记五: C#3.0自动属性,匿名属性及扩展方法
- C#学习笔记四: C#3.0自动属性&匿名属性及扩展方法
- C#中的扩展方法学习总结
- [深入学习C#]LINQ查询表达式详解(1)——基本语法、使用扩展方法和Lambda表达式简化LINQ查询
- C#温故而知新学习系列之面向对象编程—10-扩展方法
- c#学习-扩展方法
- C#特性 学习笔记(对象初始化器 匿名类型 扩展方法)
- C#可扩展编程之MEF学习笔记(三):导出类的方法和属性
- [读书笔记]C#学习笔记五: C#3.0自动属性,匿名属性及扩展方法