我正在嘗試創建一個抽象類 ThingieSection。它的派生型別將指定 ThingieSection 的型別。在 ThingieSection 的建構式中,我想要一個引數,它是 Thingie 的串列,它也是通用的。
我有一個問題,當派生類呼叫它的基類的建構式時,它認為 List 引數的型別應該是物件的型別。這是一個例子:
using System.Collections.Generic;
public abstract class ThingieSection<T>
{
public string Title { get; private set; }
public string TextStuff { get; private set; }
public List<T> Thingies { get; private set; }
protected ThingieSection(string title, string textStuff, List<Thingie<T>> Thingies)
{
}
public abstract string Generate();
}
public abstract class Thingie<T>
{
public string TextStuff { get; private set; }
protected Thingie(T workFlowEntity, string category)
{
}
public abstract string Generate();
}
public class ProviderClassyChangesProposedThingieSection : ThingieSection<ProviderClassyChangesProposedSummaryThingie>
{
public ProviderClassyChangesProposedThingieSection(string title, string content, List<Thingie<Classy>> thingies) : base(title, content, thingies)
{
}
public override string Generate() => throw new System.NotImplementedException();
}
public class ProviderClassyChangesProposedSummaryThingie : Thingie<Classy>
{
public ProviderClassyChangesProposedSummaryThingie(Classy workFlowEntity, string category) : base(workFlowEntity, category)
{
}
public override string Generate() => throw new System.NotImplementedException();
}
public abstract partial class Classy
{
}
我試過通過改變來分離型別
public abstract class ThingieSection<T>
至
public abstract class ThingieSection<T1>
和
public List<T> Thingies { get; private set; }
至
public List<T2> Thingies { get; private set; }
和ThingieSection 建構式
ThingieSection(string title, string textStuff, List<Thingie<T2>> Thingies)
但它也沒有用。
uj5u.com熱心網友回復:
我想你想要這個。您的原始 ThingieSection 僅指定一個通用引數,因此 List 和 ThingieSection 都必須使用該型別。這似乎不是您的意圖 - 您希望能夠擁有兩種型別,為此,您需要使用兩個引數。
using System.Collections.Generic;
namespace ThingsAndStuff
{
public abstract class ThingieSection<T, U>
{
public string Title { get; private set; }
public string TextStuff { get; private set; }
public List<U> Thingies { get; private set; }
protected ThingieSection(string title, string textStuff, List<Thingie<U>> Thingies)
{
}
public abstract string Generate();
}
public abstract class Thingie<T>
{
public string TextStuff { get; private set; }
protected Thingie(T workFlowEntity, string category)
{
}
public abstract string Generate();
}
public class ProviderClassyChangesProposedThingieSection : ThingieSection<ProviderClassyChangesProposedSummaryThingie, Classy>
{
public ProviderClassyChangesProposedThingieSection(string title, string content, List<Thingie<Classy>> thingies) : base(title, content, thingies)
{
}
public override string Generate() => throw new System.NotImplementedException();
}
public class ProviderClassyChangesProposedSummaryThingie : Thingie<Classy>
{
public ProviderClassyChangesProposedSummaryThingie(Classy workFlowEntity, string category) : base(workFlowEntity, category)
{
}
public override string Generate() => throw new System.NotImplementedException();
}
public abstract partial class Classy
{
}
}
uj5u.com熱心網友回復:
你可能想要一個串列Thingie<T>
public List<Thingie<T>> Thingies { get; private set; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/523370.html
標籤:C#仿制药
上一篇:檢查Rust中某些屬性的常量泛型
