【发布时间】:2013-10-04 19:11:38
【问题描述】:
我需要从一个我不知道类型的对象调用 DeepClone() 方法。
object x = GetObject();
var x2 = x as IDeepCloneable< ?????? >;//What can I do here???
var clone = x2.DeepClone();
public interface IDeepCloneable<T>
{
T DeepClone();
}
我尝试创建一个新的“基础”接口并将“:IDeepCloneable”添加到泛型类中。
public interface IDeepCloneable
{
object DeepClone();
}
但在这种情况下,我需要从 T DeepClone(); 更改派生接口的方法;到新的 T DeepClone();。结果,所有已经实现 IDeepCloneable
【问题讨论】:
-
如果您使用的是 .NET 4.0,您可以使用
dynamic来完成此操作 -
@Servy 我已经完成了...您也可以在这里使用Type.MakeGenericType...它将允许您在运行时对泛型进行专门化并将其分配给动态.
标签: c# reflection interface casting