考慮以下幾點:繼承中的循環泛型 - 爲什麼它能夠工作?
public class EntityBase<TEntity>
{
public virtual void DoSomethingWhereINeedToKnowAboutTheEntityType()
{
}
}
public class PersonEntity : EntityBase<PersonEntity>
{
public override void DoSomethingWhereINeedToKnowAboutTheEntityType()
{
}
}
我加入到這個代碼並運行它和它的工作好,但我很驚訝,我可以繼承一個類誰的定義是基於繼承類。
當我嘗試它時,我期待它不要編譯,或者一旦實際調用失敗。
你可以做一個接口類似的東西:
public interface IEntityBase<TEntity>
{}
public class PersonEntity : IEntityBase<PersonEntity>
{}
其實我已經轉換我的代碼從以前的版本,使用的界面,但我仍然好奇,爲什麼這個工程。
請注意,當您在對象上實現IComparable和IEquatable 時,您一直都在執行此操作。 –
2012-08-17 12:19:27
@FishBasketGordo。 TY編輯 - 混合我的VB和C#! – 2012-08-17 12:19:35