当前位置: 移动技术网 > IT编程>开发语言>.net > 用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用

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

2018年10月17日  | 移动技术网IT编程  | 我要评论

深圳资质转让,漂漂艺术馆,newfield

一、linq扩展方法

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

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

      二、泛型约束

  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、泛型扩展方法使用如下:

             

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

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

    

   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="李四";

  });

    

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网