在.NET 4中创建在运行时确定的类型实例的最佳方法是什么?
我有一个实例方法,虽然作用于BaseClass对象可能会被其派生类的实例调用.我需要创建this与方法中相同类型的另一个实例.为每个派生类重载Method是不切实际的,因为它相当复杂,并且更有效地保持单个实现.
public class BaseClass
{
//constructors + properties + methods etc
public SomeMethod()
{
//some code
DerivedClass d = new DerivedClass(); //ideally determine the DerivedClass type at run-time
}
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了一些关于反射或使用动态关键字但我没有这些经验.