相关疑难解决方法(0)

创建运行时确定类型的实例的最佳方法

在.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)

我已经阅读了一些关于反射或使用动态关键字但我没有这些经验.

c# reflection performance runtime dynamic

13
推荐指数
3
解决办法
1万
查看次数

标签 统计

c# ×1

dynamic ×1

performance ×1

reflection ×1

runtime ×1