VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之用LinQ扩展方法,泛型扩展方法,实现自定

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

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

  });

相关教程