zoukankan      html  css  js  c++  java
  • asp.net 高级应用 泛型generic(定义,原理,约束,缓存,协变,逆变)

    泛型generic:

    1.定义:为了达到用一套代码满足多种需求。list<int> intList=new List<int>(); intlist.where<int>(i=>i >100);
    public bool update<T>(T t) where T:BaseModel
    {
    Type type=typeof(T);
    string sql=SqlBuilder<T>.GetUpdateSql() + t.id;
    int iresult = this .ExecuteSql<int>(sql,para);
    }

    空接口是为了达到约束标记实体类型;

    泛型引入,语法,调用:
    泛型设计,原理,性能:
    泛型缓存:

    2.泛型编译原理:任何父类出现的地方都可以用子类来替换,object 是一切类的父类;object 有装箱拆箱的内存操作性能损耗,有类型安全问题;
    c#代码通过VS编译器编译为dll,exe文件,然后通过CLR/JIT编译器编译为机器码;

    泛型是asp.net 2.0出现的,包括编译器以及CLR升级;
    1.VS编译器升级,遇到泛型生成占位符;
    2.CLR升级,JIT 支持泛型,这时类型参数已确定了,所以JIT能编译生成确定一个类型的代码;
    那有什么岁月静好,只不过有人在替你负重前行。只是.net 平台进步了。
    做人应:仁义礼智信,温良恭俭让。


    3.泛型各种应用:
    泛型方法: show<T>(T tparameter),一个参数满足不同类型,声明时不指定类型,调用方法时指定类型,称为延迟
    架构师流行:推迟一切可以的推迟的。

    public class GenericCache<T>

    GenericCache<string>.GetData();
    GenericCache<int>.GetData();
    GenericCache<DateTime>.GetData();
    GenericCache<object>.GetData();
    GenericCache<GenericCacheTestClass>.GetData();

    4.泛型约束:
    1.基类约束:同一个类或子类,只能一个
    public static T show<T>(T tparameter) where T:People
    {
    console.writeLine(tparameter.id);
    console.writeLine(tparameter.name);
    tparameter.breath();
    }
    2.接口约束:可以多个接口
    public static T show<T>(T tparameter) where T:People,Isport
    {
    console.writeLine(tparameter.id);
    console.writeLine(tparameter.name);
    tparameter.breath();
    }
    3.引用类型约束:
    public static T show<T>(T tparameter) where T:class
    {
    return null;
    }
    4.值类型约束:
    public static T show<T>(T tparameter) where T:struct
    {
    return default(T);//根据T的类型返回默认值;
    }

    5.无参数构造函数约束:
    public static T show<T>(T tparameter) where T:new()
    {
    return new T();
    }

    //如果直接用父类做参数,相对于泛型+约束 会少了灵活性,做不到多重约束。
    showparent(people tparameter)
    {
    console.writeLine(tparameter.id);
    console.writeLine(tparameter.name);
    tparameter.breath();
    }
    基类约束:
    1.权利:可以直接在方法里面是有基类的东西;
    2.义务:类型参数必须满足约束;

    GenericConstraint.Show<people>


    泛型缓存:类型缓存,int,string等类型第一次都会执行,第二次执行时读缓存(不会重新执行构造函数),性能高;
    字典:hash寻址(KEY VAVLUE)比较快,内存空间用的多;
    List:数组结构,不适合增删改;


    泛型类:
    public class child<T>:GenericClass<T>,GenericInterface<T>
    public class childC:GenericClass<string>,GenericInterface<int>

    泛型接口:
    public class childC:GenericClass<string>,GenericInterface<int>

    5.泛型委托:

    6.协变逆变.net 4.0:只能在接口和委托上
    协变:可以左边类型参数是父类,右边参数是子类;要求T只能是返回值,不能是参数;
    out :协变;
    public interface IcuOUT<out T>
    {
    T GET();
    }


    in 逆变,修饰传入参数;可以左边类型参数是子类,左边类型参数是父类
    要求T 只能是作为参数,不能作为返回值

    public interface IcuIn<in T>
    {
    //T GET();
    void Show(T t);
    }


    协变,逆变是解决这个问题的
    List<Bird> blist=new List<maque>();//编译不过


    逆变,协变一起用:逆变是指定传参类型,协变是指定返回值类型
    public interface Ilist<int inT,out outT>
    {
    void show(inT t);
    outT Get();
    outT Do(inT t);
    }
    Action<int,string>


    //generic泛型解决了装箱拆箱,类型安全问题
    public static void show<T>(T tparameter)
    {
    Console.WriteLine("this is show<T>,PARAMETER={0} type={1}",tparameter,tparameter.GetType());
    }

    //有装箱拆箱,类型安全问题
    public static void show<T,A,B,C>(T tparameter,A a,B b,C c)
    {
    Console.WriteLine("this is show<T>,PARAMETER={0} type={1}",tparameter,tparameter.GetType());
    Console.WriteLint(a,b,c);
    }

    //object 是一切类的父类;object 有装箱拆箱的内存操作性能损耗,有类型安全问题;
    publ static void showobject(object oparameter)
    {
    Console.WriteLine("this is show<T>,PARAMETER={0} type={1}",oparameter,oparameter.GetType());
    }

  • 相关阅读:
    【52】目标检测之检测算法
    【51】目标检测之特征点检测
    6-----Scrapy框架中Item Pipeline用法
    5-----Scrapy框架中Spiders用法
    4-----Scrapy框架中选择器的用法
    3-----Scrapy框架的命令行详解
    1-----Scrapy框架整体的一个了解
    Python入妖5-----正则的基本使用
    win安装wordcloud报错解决方案
    在新项目下使用rbc权限
  • 原文地址:https://www.cnblogs.com/csj007523/p/13418342.html
Copyright © 2011-2022 走看看