比如有这样一个泛型:Demo.GenericsSimple<T,TT> 我想要通过反射创建一个Demo.GenericsSimple<string,int>的实例可以通过下面的格式进行创建:

System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("命名空间.User`形参数量N[[1形参类型全名,形参类型所在的程 序集名称],[2形参类型全名,形参类型所在的程序集名称],[3形参类型全名,形参类型所在的程序集名称]......N]")

代码:

namespace Demo
{
    public class GenericsSimple<T,TT> where T:class where TT:struct
    {}

    class Program
   {
        static void Main(string[] args)
        {
           var o = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("Demo.GenericsSimple`2[[System.String, mscorlib],[System.Int32, mscorlib]]");
 Console.WriteLine("{0}",o==null?"null":o.GetType().FullName);
            Console.WriteLine("===end===");
            Console.Read();       

     }
    }

}

 

 

 

相关文章:

  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
  • 2021-06-23
  • 2022-01-12
  • 2022-12-23
  • 2021-08-29
猜你喜欢
  • 2021-06-25
  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案