我有下面的類C#.NET 3.5獲取一般定義
public class AbstractJobDao<T> where T : IJob {
protected T buildJob() {
// create an IJob implementation instance based on the generic parameter
}
}
public class FullTimeJobDao : AbstractJobDao<FullTimeJob> {
}
public class InternshipDao : AbstractJobDao<Internship> {
}
兩個FullTimeJob和實習實施IJob接口。我希望buildJob()方法能夠使用反射來推斷Dao實現類中的泛型,然後創建該類型的實例。有沒有辦法在.NET 3.5中使用反射來做到這一點?
如果是這樣,buildJob()方法中代碼的行/行應該是什麼樣子?
編輯 - 我想我不清楚我的問題。我想要的是當在FullTimeJobDao中調用buildJob()時創建FullTimeJob的實例。當從InternshipDao內部調用buildJob()時,它應該根據類頂部定義的泛型類型創建一個實習實例。
Type.GenericTypeArguments + Activator.CreateInstance() –
上面的voroninp的評論是我在這裏尋找的答案。謝謝! –