欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#中的扩展方法学习笔记

程序员文章站 2023-12-14 19:51:28
前言 最近在看王清培前辈的.net框架设计时,当中有提到扩展方法 . 开头的一句话是:扩展方法是让我们在不改变类原有代码的情况下动态地添加方法的方式,这给面向对象设计...

前言

最近在看王清培前辈的.net框架设计时,当中有提到扩展方法 .

开头的一句话是:扩展方法是让我们在不改变类原有代码的情况下动态地添加方法的方式,这给面向对象设计 模块设计带来了质的提升

很明显,扩展方法在框架设计或者平时码代码中,是能够提升我们整个架构的灵活性的

简介

扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。 扩展方法当然不能破坏面向对象封装的概念,所以只能是访问所扩展类的public成员。

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

c#扩展方法第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。

扩展方法的目的就是为一个现有类型添加一个方法,现有类型既可以是int,string等数据类型,也可以是自定义的数据类型。

一..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请求,在进行拼装请求报文时,就会使用到链式编程

如下代码所示

c#中的扩展方法学习笔记 

以上代码就是使用了扩展方法进行链式编程,从而使得整个请求信息可以在一句代码中体现出来

接下来,我们自己实现链式代码

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; }

 }

调用和执行结果

 c#中的扩展方法学习笔记

总结

1.使用扩展方法能在不修改原有类型的基础下,动态添加方法,这使得整个框架更具有灵活性

2.在使用上下文信息的时候,可以使用链式编程,使得调用时能够在一句代码中完成所有属性设置

3.扩展方法不能滥用.添加扩展方法应当使用最小影响原则,即尽量不要在父类使用扩展方法,比如object,这将影响性能

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

上一篇:

下一篇: