美文网首页
2019-10-14 【c#】反射:泛型反射:MakeGener

2019-10-14 【c#】反射:泛型反射:MakeGener

作者: 持刀的要迟到了 | 来源:发表于2019-10-14 16:04 被阅读0次

https://www.cnblogs.com/OpenCoder/p/5957668.html

using System;
using System.Reflection;
using static RFTest.ReflectionTest;

namespace RFTest
{
    //类ReflectionTest中定义了一个泛型函数DisplayType和泛型类MyGenericClass
    class ReflectionTest
    {
        //泛型类MyGenericClass有个静态函数DisplayNestedType
        public class MyGenericClass<T>
        {
            public static void DisplayNestedType()
            {
                Console.WriteLine(typeof(T).ToString());
            }

            public void DisplayType()
            {
                Console.WriteLine(typeof(T).ToString());
            }
        }

        public void DisplayType<T>()
        {
            Console.WriteLine(typeof(T).ToString());
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ReflectionTest rt = new ReflectionTest();

            //先获取到DisplayType<T>的MethodInfo反射对象
            MethodInfo mi = rt.GetType().GetMethod("DisplayType");

            //然后使用MethodInfo反射对象调用ReflectionTest类的DisplayType<T>方法,
            //这时要使用MethodInfo的MakeGenericMethod函数指定函数DisplayType<T>的泛型类型T
            mi.MakeGenericMethod(new Type[] { typeof(string) }).Invoke(rt, null);

            //这里获取MyGenericClass<T>的Type对象,注意GetNestedType方法的参数要用MyGenericClass`1这种格式才能获得MyGenericClass<T>的Type对象
            Type myGenericClassType = rt.GetType().GetNestedType("MyGenericClass`1");
            //然后用Type对象的MakeGenericType函数为泛型类MyGenericClass<T>指定泛型T的类型,比如上面我们就用MakeGenericType函数将MyGenericClass<T>指定为了MyGenericClass<float>,然后继续用反射调用MyGenericClass<T>的DisplayNestedType静态方法
            myGenericClassType.MakeGenericType(new Type[] { typeof(float) }).GetMethod("DisplayNestedType", BindingFlags.Static | BindingFlags.Public).Invoke(null, null);


            Type myGenericClassType2 = typeof(MyGenericClass<>);
            myGenericClassType2.MakeGenericType(typeof(double)).GetMethod("DisplayNestedType").Invoke(null, null);


            Type myGenericClassType3 = typeof(MyGenericClass<>);
            // 非静态方法,需要先创建实例调用
            //myGenericClassType2.MakeGenericType(typeof(double)).GetMethod("DisplayType").Invoke(null, null);
            Type geneTp = myGenericClassType3.MakeGenericType(typeof(int));
            MyGenericClass<int> obj = Activator.CreateInstance(geneTp) as MyGenericClass<int>;
            obj.DisplayType();
            object obj2 = Activator.CreateInstance(geneTp);
            MethodInfo mi2 = obj2.GetType().GetMethod("DisplayType");
            mi2.Invoke(obj2, null);


            Console.ReadLine();
        }
    }
}
image.png

相关文章

  • 2019-10-14 【c#】反射:泛型反射:MakeGener

    https://www.cnblogs.com/OpenCoder/p/5957668.html

  • 目录 - C#

    总目录 C# 第01局:泛型 C# 第02局:反射 C# 第03局:特性 C# 第04局:委托 C# 第05局:事...

  • 【Java】【反射】泛型反射

    泛型反射 在运行时,泛型是无效的,所以可以通过反射在运行时将其他类型变量添加到集合,而不需要考虑泛型

  • 泛型

    ORM实现有反射、泛型、代码生成等几种常见方式,或者单用,或者混合。 c#的泛型非常强大,应用于ORM时,可能有些...

  • 泛型

    ORM实现有反射、泛型、代码生成等几种常见方式,或者单用,或者混合。 c#的泛型非常强大,应用于ORM时,可能有些...

  • 关于反射的使用

    反射中获取泛型参数信息

  • 泛型 & 注解 & Log4J日志组件

    掌握的知识 : 基本用法、泛型擦除、泛型类/泛型方法/泛型接口、泛型关键字、反射泛型(案例) 泛型 概述 : 泛型...

  • 基础知识巩固——反射、注解、泛型

    内容大纲 反射注解泛型 反射 参考文章:https://blog.csdn.net/sinat_38259539/...

  • Java反射 - 泛型

    使用Java泛型通常分为两种不同的情况: 声明一个类/接口是可参数化的。 使用可参数化的类。当你写一个类或接口时,...

  • java 泛型 反射

    通过类上指定的泛型 测试类 说明 1、new TestCall2 () { }是新建了个子类 其类型class ...

网友评论

      本文标题:2019-10-14 【c#】反射:泛型反射:MakeGener

      本文链接:https://www.haomeiwen.com/subject/kmermctx.html