C#如何使用反射实现通过字符串创建类

来源:互联网 发布:c语言找假币 编辑:程序博客网 时间:2024/06/05 18:23
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ReflectFromStringToClass{    class Program    {        static void Main(string[] args)        {                        string str = "A";            Type type = Type.GetType(new Program().GetType().Namespace+"."+str,true,true);            var temp= Activator.CreateInstance(type);            A a = temp as A;            a.PintA();                    }    }    public class A    {        public A()        {        }        public void PintA()        {            Console.WriteLine("A");        }    }       }

0 0