• 读书笔记 C# Type类型与泛型有关的某些属性浅析


    IsGenericType 如果类型为泛型,则返回 true。
    GetGenericArguments

    返回 Type 对象数组,这些对象表示为构造类型提供的类型变量,或泛型类型定义的类型参数。
    如果是MyList<int,Person> ,则返回int和Person类型的数组,如同Type[] tpyes={typeof(int),typeof(Person)},Type数组中任一参数的IsGenericParameter为false;
    如果是MyList<,>或,则返回T和U类型的数组,这时得到的Type数组中的任一参数的IsGenericParameter属性为true,因为MyList<T,U>是泛型类型的初始定义

    GetGenericTypeDefinition

    返回当前构造类型的基础泛型类型定义。
    返回一个基础定义的Type,如MyList<T,U>,可以用来构造其他泛型类型定义。
    Type t = typeof(Stack<>);
    //获取基础泛型类型定义,即Stack<T>,这时可用defType定义其他的泛型类型,比如Stack<Person>
    Type defType = t.GetGenericTypeDefinition();

    if(defType.IsGenericTypeDefinition) {
    Type dType = defType.MakeGenericType(typeof(Person));
    Console.WriteLine(dType.IsGenericTypeDefinition);
    }

    GetGenericParameterConstraints

    如果泛型类型的类型参数T有参数约束,且这些约束参数是自定义的类型(如Person)或其他可以用来实例化对象的类型,不是class、struct等关键字的话,则可用该方法获取这些参数约束的类型。该方法会返回一个Type数组。
    如:
    //这个会返回Type[] type={typeof(Person)}
    public class Stack<T> where T:Person {}

    //这个返回的Type数组的length为0
    public class Stack<t> where T:class{}

    ContainsGenericParameters

    如果类型或其任意封闭类型或方法包含没有被提供特定类型的类型参数,则返回 true。
    即:
    //提供了特定类型参数Person
    Type t = typeof(Stack<Person>);
    //返回false
    Console.WriteLine(t.ContainsGenericParameters);

    //没有提供特定类型参数
    Type t = typeof(Stack<>);
    //返回true
    Console.WriteLine(t.ContainsGenericParameters);

    GenericParameterAttributes 获取 GenericParameterAttributes 标志的组合,这些标志描述当前泛型类型参数的特殊约束。如class、new()等
    GenericParameterPosition

    对于表示类型参数的 Type 对象,获取类型参数在声明该类型参数的泛型类型定义或泛型方法定义的类型参数列表中的位置。
    如果IsGenericParameter为false,表明用于typeof的Stack泛型的类型参数是特定的,不是原生的T,这时不能使用GenericParameterPosition属性,会抛出异常;如果IsGenericParameter为true,才能使用GenericParameterPosition属性。
    //首先通过GetGenericArguments方法获取,如:
    Type t = typeof(Stack<>);
    Type[] types = t.GetGenericArguments();
    //其次可用foreach循环。如果是typeof(Stack<Person>)这种情况,t1.IsGenericParameter会为false,这时不能使用t1.GenericParameterPosition属性,否则会抛出异常。
    foreach(Type t1 in types) {
    Console.WriteLine(t1.Name + " " + t1.IsGenericParameter + " "+t1.GenericParameterPosition);
    }

    IsGenericParameter

    获取一个值,该值指示当前 Type 是表示泛型类型定义的类型参数,还是泛型方法定义的类型参数。
    如果typeof的是原生的泛型类(例如Type type=typeof(Stack<>);),则IsGenericParameter为true,否则为false

    IsGenericTypeDefinition

    获取一个值,该值指示当前 Type 是否表示可以用来构造其他泛型类型的泛型类型定义。 如果类型表示泛型类型的定义,则返回 true。
    我的理解是,是否为原生的泛型类型定义,即是否为这样的形式定义的Type:
    Type type=typeof(Stack<>);
    如果是,则返回true,否则返回false。因为定义的type,才是原生的泛型类型,才能通过以下代码去定义特定类型的泛型类:
    if(type.IsGenericTypeDefinition) {
    //dType如同Type dtype=typeof(Stack<Person>);
    Type dType = type.MakeGenericType(typeof(Person));
    }

    DeclaringMethod 返回定义当前泛型类型参数的泛型方法;如果类型参数不是由泛型方法定义的,则返回空值。
    MakeGenericType 用类型数组的元素替代当前泛型类型定义的类型参数,并返回表示结果构造类型的 Type 对象。

       

    发现一个有趣的现象,如果属性中包含Generic字眼的,多数都是指明Type为typeof的原生泛型类,及Type type=typeof(Stack<>);这种形式,原生的也称为基础的泛型类型可以用来定义特定类型的其他泛型类,即用可以实例化对象的类型将T替换掉。

  • 相关阅读:
    switch 是否能作用在byte 上,是否能作用在long 上,是否能作用在String上?
    int和Integer有什么区别?
    访问修饰符public,private,protected,以及不写(默认)时的区别?
    Hashmap如何同步?
    Iterator和ListIterator的区别?
    Java的HashMap是如何工作的?
    MyBatis与Hibernate有哪些不同?
    Spring框架中有哪些不同类型的事件?
    Spring的自动装配?
    Spring如何处理线程并发问题?
  • 原文地址:https://www.cnblogs.com/williamwsj/p/6086274.html
Copyright © 2020-2023  润新知