Skip to content

Add MethodInfo.MakeGenericMethod() and Type[].Where() or IEnumerable<Type>.Where() #1030

@KasperJSdeVries

Description

@KasperJSdeVries

Description

MakeGenericMethod

I need to call a generic function with a type decided at runtime, the only way to do this in c# is to use MakeGenericMethod with a type.

*.Where()

I often need a list of all methods that implement a certain interface and to do this it is very convenient to use .where instead of looping through all of the classes manually.

How to solve the problem

MakeGenericMethod()

MakeGenericMethod needs an implementation.

*.Where()

the IEnumerable interface needs a Where method

Describe alternatives you've considered

MakeGenericMethod

I believe that there are no alternatives to this function in the c# standard library

*.Where()

You can manually loop through all the items in the list and filter them that way.

Aditional context

example MakeGenericMethod use:

MethodInfo method = typeof(Sample).GetMethod(nameof(Sample.GenericMethod));
MethodInfo generic = method.MakeGenericMethod(myType);
generic.Invoke(this, null);

example *.where use:

var typesImplementingInterface = typeof(Program).Assembly.Types.GetEnumerable.Where(t => t.IsSubClassOf(IexampleInterface));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions