您的位置:首页 > 编程语言 > C#

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

2017-01-03 17:09 309 查看
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");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐