-1

I have an abstract class with a few or many (from 1 all the way up to 16) generic parameters like so:

public abstract class ComponentSystem<F1> : ComponentSystemBase
    where F1 : IComponent
{ }

public abstract class ComponentSystem<F1, F2> : ComponentSystemBase
    where F1 : IComponent
    where F2 : IComponent
{ }

public abstract class ComponentSystem<F1, F2, F3> : ComponentSystemBase
    where F1 : IComponent
    where F2 : IComponent
    where F3 : IComponent
{ }

public abstract class ComponentSystem<F1, F2, F3, F4> : ComponentSystemBase
    where F1 : IComponent
    where F2 : IComponent
    where F3 : IComponent
    where F4 : IComponent
{ }

Now I have a base class that contains a list of types like so:

public abstract class ComponentSystemBase
{
    public List<Type> Filters { get; private set; }
    public abstract void Init();
    public abstract void Update();
    protected internal void PopulateFilterList(Type[] filters)
    {
        // oneliner?
        foreach (Type filter in filters)
        {
            Filters.Add(filter);
        }
    }
}

Now my question is, how do I get all the generic parameters in a type array so I can pass it to my function who adds it to the list?

Is there a function to get all the generic parameters, do I have to do it manually for each class?

If something is unclear let me know so i can clarify!

Trevor
  • 7,777
  • 6
  • 31
  • 50
FutureCake
  • 2,614
  • 3
  • 27
  • 70
  • 5
    if all parameters are such as `where F1 : IComponent` why don't you use a `List` ? – Cid Oct 14 '19 at 13:14
  • 2
    Aside from truly generic things like `Func`, `Action` and `ValueTuple` which have good use cases, having long lists of generic arguments is probably a code smell. Note that all of the aforementioned classes have compiler support to easily instantiate them. – Jeroen Mostert Oct 14 '19 at 13:16
  • Do you want the parameters ("F1") that the generic class is willing to accept, or the actual arguments ("String") that were provided? – 15ee8f99-57ff-4f92-890c-b56153 Oct 14 '19 at 13:17
  • @Cid I do it because for the futher implementation it looks nicer :p It is part of an entity component system i am writing. But suggestions are always welcome! – FutureCake Oct 14 '19 at 13:17
  • @EdPlunkett I am not sure what you mean :( I am a newb when it comes to c# and programming :) – FutureCake Oct 14 '19 at 13:19
  • @EdPlunkett I don't doubt it haha, Always when i try to make something i sink deeper and deeper the further the project progresses ;) – FutureCake Oct 14 '19 at 13:22
  • I'm actually quite surprised that this doesn't have a dedicated Stack Overflow Q&A. Only thing I found was [this](https://stackoverflow.com/q/557340/9363973) but the accepted answer on it doesn't answer getting all generic parameters from a class – MindSwipe Oct 14 '19 at 13:23
  • Possible duplicate of [Get all types implementing specific open generic type](https://stackoverflow.com/questions/8645430/get-all-types-implementing-specific-open-generic-type) **or** https://stackoverflow.com/questions/557340/how-to-get-the-type-of-t-from-a-member-of-a-generic-class-or-method – Trevor Oct 14 '19 at 13:30

1 Answers1

3

Reflection to the rescue!

Type[] types = GetType().GetGenericArguments();
MindSwipe
  • 7,193
  • 24
  • 47