public class SomeClass<T>
{
public T DoSomething()
{
var obj = Activator.CreateInstance(this.GetType());
// Do Things
return (T)obj;
}
}
public class DoStuff : SomeClass<DoStuff>
{
}
var ds = new DoStuff();
ds.DoSomething();
這個作品,但在這種情況下:C#反射投射到實例類型爲前變種O =(typeof運算(本))anotherobj
public abstract class AbstractStuff : SomeClass<AbstractStuff>
{
}
public class DoStuff : AbstractStuff
{
}
var ds = new DoStuff();
ds.DoSomething();
給我一個錯誤,不能實例化一個抽象一流的,我理解,我想做的事情是這樣的:
public class SomeClass<T>
{
public T DoSomething()
{
var obj = Activator.CreateInstance(this.GetType());
// Do Things
return (my not know how to get this casting type)obj;
}
}
我tryed 回報(typeof運算(本))目標文件;
並沒有工作,一些想法? 感謝名單
SORRY:我忘了申報AbstractStuff抽象(修正)
是不是失敗的CreateInstance? – kaj 2012-02-23 14:36:37
不,CreateInstance正在工作,返回類型必須是(不)ç抽象,DoStuff不是抽象的,但T tyoe是AbstractStuffm我需要轉換爲此類型,thanx – manuellt 2012-02-23 14:38:55
是AbstractStuff抽象的嗎? – 2012-02-23 14:43:09