Home > Blockchain >  Multiple Generic Types in a C# abstract class
Multiple Generic Types in a C# abstract class

Time:10-28

I'm trying to create an abstract class ThingieSection. It's derived types will specify the type of ThingieSection. In the constructor of ThingieSection I want a parameter that's a list of Thingie which is also generic.

I'm having a problem in that when the derived class is calling it's base class's constructor, it thinks that the type of the List parameter should be the type of the object. Here's an example:

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
    {

    }

I've tried separating the types by changing

public abstract class ThingieSection<T>

to

public abstract class ThingieSection<T1>

and

public List<T> Thingies { get; private set; } 

to

public List<T2> Thingies { get; private set; }

and the ThingieSection constructor
to

ThingieSection(string title, string textStuff, List<Thingie<T2>> Thingies)

but it didn't work either.

CodePudding user response:

I think you want this. Your original ThingieSection specifies only one generic parameter, so the List and the ThingieSection must both use that type. That doesn't seem to be your intent - You want to be able to have two types, and to do that, you'll need to use two parameters.

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
    {

    }
}

CodePudding user response:

You probably you want a list of Thingie<T>

public List<Thingie<T>> Thingies { get; private set; }
  • Related