scottmgerstl
scottmgerstl

Reputation: 765

How to inherit from a list of generic objects

I am getting the error Using the generic type GetOption<EnumT> requires 1 type argument. The error is coming from the first line where I am inheriting from List<GetOption>

I am trying to set up an inheritance structure where I can do all the processing at the base objects and have the specifics reside in the child objects. The number of child objects are N in number. The base objects will need to be informed of the child types at instantiation.

The parent-most class will inherit from a List so I can have the functionality of a list in the parent-most object for adding generic children whose type will be informed at instantiation.

Here is what I have so far:

The first two classes are the base classes

public abstract class GetOptions : List<GetOption>
{
    public enum FilterDirections
    {
        Ascending,
        Descending
    }

    private FilterDirections _filterDirection;

    public FilterDirections FilterDirection
    {
        get { return _filterDirection; }
        set { _filterDirection = value; }
    }

    public GetOptions()
    {
        FilterDirection = FilterDirections.Ascending;
    }
}

public abstract class GetOption<EnumT>
{
    private EnumT _column;

    public EnumT Column
    {
        get { return _column; }
        set { _column = value; }
    }

    public GetOption()
    {

    }
}

The last two classes are sample child classes

public class PersonTypeLookupGetOptions : GetOptions
{
    public PersonTypeLookupGetOptions() : base() { }
}

public class PersonTypeLookupGetOption : GetOption<PersonTypeLookup.PersonTypeLookupFields>
{

}

How would I accomplish what I am trying to do?

EDIT:

What I am looking to do is define the type of object within the List I am inheriting from in the child objects.

With the comment under my first question, I was able to come to the solution I needed. The Class definitions are below:

public abstract class GetOptions<ChildT, EnumT> : List<ChildT> where ChildT : GetOption<EnumT>

public abstract class GetOption<EnumT>

public class PersonTypeLookupGetOptions : GetOptions<PersonTypeLookupGetOption, PersonTypeLookup.PersonTypeLookupFields>

public class PersonTypeLookupGetOption : GetOption<PersonTypeLookup.PersonTypeLookupFields>

Upvotes: 0

Views: 125

Answers (2)

evanmcdonnal
evanmcdonnal

Reputation: 48134

Although I do not know a solution to your bigger problem I'm fairly certain your error is due to you using List<GetOption> while the compiler expects List<GetOption<EnumT>>. That being said I don't really understand what you're going for here...

Edit: it seems more like you just want GetOption to be a class without the <EnumT> part and then you want to inherit from List<GetOption>. So changing the base class to GetOption instead of GetOption<EnumT> may solve your problems.

Upvotes: 0

D Stanley
D Stanley

Reputation: 152634

It's not clear what you're trying to do, but one thing that may help you is to propagate the generic parameter to the collection type:

public abstract class GetOptions<EnumT> : List<GetOption<EnumT>>

The parent-most class will inherit from a List so I can have the functionality of a list in the parent-most object for adding generic children whose type will be informed at run-time

Be aware that generics are bound at compile-time, not at run-time.

Upvotes: 2

Related Questions