C#根据输入的字符串来创建类的实例
2021-02-12 03:17
标签:bst activator creat ogr har tty err span lin 运行结果: C#根据输入的字符串来创建类的实例 标签:bst activator creat ogr har tty err span lin 原文地址:https://www.cnblogs.com/bruce1992/p/14394945.html abstract class Vehicle
{
public abstract void Drive();
}
class Car : Vehicle
{
public override void Drive()
{
Console.WriteLine("Car is driving...");
}
}
class Bus : Vehicle
{
public override void Drive()
{
Console.WriteLine("Bus is driving...");
}
}
class Program
{
static void Main(string[] args)
{
string typeName = Console.ReadLine();
Type type = Type.GetType(new Program().GetType().Namespace + "." + typeName, true, true);
Vehicle vehicle = (Vehicle)Activator.CreateInstance(type);
vehicle.Drive();
Console.ReadKey();
}
}