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

用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用

程序员文章站 2022-03-10 14:40:56
一、Linq扩展方法 1、扩展方法必须是静态方法、扩展方法所在的类必须是静态类 2、扩展方法里面的参数必须制定this关键字,紧跟需要扩展的类型,如下: 二、泛型约束 1、使用泛型的原因,是在不知道需要扩展的类型时(这里主要时指对象类型,比如:可能时student类型,可能时person类型)前提下 ......

一、linq扩展方法

    1、扩展方法必须是静态方法、扩展方法所在的类必须是静态类

              2、扩展方法里面的参数必须制定this关键字,紧跟需要扩展的类型,如下:

      用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用二、泛型约束

  1、使用泛型的原因,是在不知道需要扩展的类型时(这里主要时指对象类型,比如:可能时student类型,可能时person类型)前提下使用泛型,但使用泛型时需要加约束

       2、泛型约束常用支持以下几个

    where t : struct              t必须是一个结构类型
    where t : class               t必须是一个类(class)类型,不能是结构(struct)类型
    where t : new()               t必须要有一个无参构造函数
    where t : nameofbaseclass     t必须继承名为nameofbaseclass的类
    where t : nameofinterface     t必须实现名为nameofinterface的接口

       3、泛型扩展方法使用如下:

             用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用

三、扩展方法里面使用lamada表达式处理代码逻辑。这里用到了action委托,action无返回值,参数可有可无,也可以用func委托(两者区别——>百度),本质都是把委托作为方法的参数传递,类似于方法的回调。

  1、代码如下,t可以是任意类型,但由于方法加了class,new()约束,所以t只能是一个类类型且该类型必须有一个无参构造函数

    用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用

   2、泛型扩展方法的使用

      student.isnull(o=>{

        o.id="1001"

        o.name="张嘞个三"

      })

 四、扩展方法demo,整个源码,可以直接用,需要扩展其他类型,往里面追加

  //扩展方法所在的类必须是静态类

  public static class extendmethod

  {
    public static bool isnotempty(this string obj, action action)
    {
      bool result = string.isnullorempty(obj);
      if (!result)
      {
        action();
      }
      return result;
    }
    public static bool isnotempty(this string obj, action<string> action)
    {
      bool result = string.isnullorempty(obj);
      if (!result)
      {
        action(obj);
      }
      return result;
    }
    public static bool isempty(this string obj, action action)
    {
      bool result = string.isnullorempty(obj);
      if (result)
      {
        action();
      }
      return result;
    }

    public static bool isempty(this string obj, action<string> action)
    {
      bool result = string.isnullorempty(obj);
      if (result)
      {
        action(obj);
      }
      return result;
    }

    public static bool isnotnull<t>(this t t, action action)
      where t : class, new()
    {
      bool result = t == null;
      if (!result)
      {
        action();
      }
      return result;
    }

    public static bool isnotnull<t>(this t t, action<t> action)
      where t : class , new()
    {
      bool result = t == null;
      if (!result)
      {
        action(t);
      }
      return result;
    }

    public static bool isnull<t>(this t t, action action)
      where t : class ,new()
    {
      bool result = t == null;
      if (result)
      {
        action();
      }
      return result;
    }

    public static bool isnull<t>(this t t, action<t> action)
      where t : class ,new()
    {
      bool result = t == null;
      if (result)
      {
        action(t);
      }
      return result;
    }

  }

五、以上demo示列的用法如下:

 1、扩展string类型判断字符是否为空

   string m = string.empty;

  m.isnotempty(() =>
  {
    m = "1";
  });

  m.isnotempty(o =>
  {
    m = f;
  });

 2、泛型扩展类型,判断对象是否为null
  student stu=new student();

  //这里括号指匿名方法,可以理解成占位符

  stu.isnull(() => { 

    stu.id="1003";

    stu.name="李四";

   });
  

  stu.isnull(o => {

    

    o.id="1003";

    o.name="李四";

   });

  
  stu.isnotnull(() => {

    stu.id="1003";

    stu.name="李四";

 

  });

  stu.isnotnull(o => {

    o.id="1003";

    o.name="李四";

  });