我有以下代码:
public class Foo
{
public void DoSomething()
{
DoSomething(this);
}
private static void DoSomething<T>(T obj)
{
var generic = new Generic<T>();
}
}
public class Bar : Foo
{
// properties/methods
}
public class Generic<T>
{
// properties/methods
}
public class Test
{
public void TestMethod()
{
var bar = new Bar();
bar.DoSomething(); // instantiates Generic<Foo> instead of Generic<Bar>
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用当前类型而不是基类型从派生方法实例化泛型类?