通过 Activator.CreateInstance 以及 继承关系实现
public interface Name
{
String GetName();
}
public class Join : Name
{
public string GetName()
{
return "Join";
}
}
public class Thomas : Name
{
public string GetName()
{
return "Thomas";
}
}
public class RecongisePerson
{
Hashtable nameTable = new Hashtable();
public void SetNameList(String name, Type type)
{
nameTable[name] = type;
}
public String GetName(String person)
{
Name b = (Name)Activator.CreateInstance((Type)nameTable[person]);
String s = b.GetName();
return s;
}
}