實際上這個不是完全可以的

來源: 2009-09-14 09:35:38 [舊帖] [給我悄悄話] 本文已被閱讀:

這個是可以的:
MyClass.Animal animal = new MyClass.Wolf();
MyClass.Wolf wolf = animal as MyClass.Wolf;
wolf.Eat(); //但是-> "I eat like a wolf"

Reflection sample, but still cannot make impossible to possible:

System.Reflection.Assembly r = System.Reflection.Assembly.LoadFrom(@"C:\MyProjects\TEST\Reflection\Reflection\bin\Debug\MyClass.dll");
Type[] ts = r.GetTypes();
foreach (Type t in ts)
{
if (t.Name == "Animal")
{
MyClass.MyClass.Animal animal = new MyClass.MyClass.Wolf();
MyClass.MyClass.Animal wolf = Activator.CreateInstance(t) as MyClass.MyClass.Animal;
wolf.Eat();
Console.ReadLine();
}
}