泛型反射- 泛型方法

来源:互联网 发布:个人业务网站源码 编辑:程序博客网 时间:2024/04/29 04:43
using System;using System.Reflection;using System.Collections.Generic;namespace ConsoleTest{    class Program    {              static void Main(string[] args)        {            ////Console.Write(typeof(Pair<int,string>).ToString());            Assembly asm = Assembly.GetExecutingAssembly();                         //string pair1 = "ConsoleTest.Pair";            //string pair2="ConsoleTest.Pair`2";            string pair3 = "ConsoleTest.PairUse";            Type type = asm.GetType(pair3, true);            //type = type.MakeGenericType(typeof(int), typeof(string));            //Console.WriteLine(type.IsGenericType);             //ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(int), typeof(string) });            object obj = asm.CreateInstance(pair3);// ctor.Invoke(new object[] { 1, "wth" });            MethodInfo method = type.GetMethod("MakePair");            method = method.MakeGenericMethod(typeof(int), typeof(string));            object pu=  method.Invoke(obj, new object[] { 1, "wtf" });            Console.ReadLine();        }           }    public class Pair<T, S> {        public T first;        public S second;        public Pair(T first, S second) {            this.first = first; this.second = second;        }            }    public class PairUse {        public Pair<T, S> MakePair<T, S>(T t, S s)         {             return new Pair<T, S>(t, s);        }    }        public class Pair {        public object first;        public object second;        public Pair(object first, object second)        {            this.first = first; this.second = second;        }    }}

原创粉丝点击