Calling a generic method with a dynamic type

C#Generics

C# Problem Overview


Lets say I have the following classes

public class Animal { .... }

public class Duck : Animal { ... }

public class Cow : Animal { ... }

public class Creator
{
   public List<T> CreateAnimals<T>(int numAnimals)
   {
      Type type = typeof(T);
      List<T> returnList = new List<T>();
      //Use reflection to populate list and return
   }
}

Now in some code later I want to read in what animal to create.

Creator creator = new Creator();
string animalType = //read from a file what animal (duck, cow) to create
Type type = Type.GetType(animalType);
List<animalType> animals = creator.CreateAnimals<type>(5);

Now the problem is the last line isn't valid. Is there some elegant way to do this then?

C# Solutions


Solution 1 - C#

I don't know about elegant, but the way to do it is:

typeof(Creator)
    .GetMethod("CreateAnimals")
    .MakeGenericMethod(type)
    .Invoke(creator, new object[] { 5 });

Solution 2 - C#

Not really. You need to use reflection, basically. Generics are really aimed at static typing rather than types only known at execution time.

To use reflection, you'd use Type.GetMethod to get the method definition, then call MethodInfo.MakeGenericMethod(type), then invoke it like any other method.

Solution 3 - C#

Try this:

public List<T> CreateAnimals<T>(int numAnimals) where T : Animal
{
    Type type = typeof(T);
    List<T> returnList = new List<T>();
    //Use reflection to populate list and return
}

It should make sure that the allowed types for CreateAnimals inherit from Animal. Then hopefully, it won't have a problem with List<animalType> animals = creator.CreateAnimals<type>(5);

Solution 4 - C#

The keys to this are MakeGenericType() and MakeGenericMethod(). Once you've gone dynamic with the types, you can't really go back to static typing. What you CAN do is create the list dynamically, by using Activator.CreateInstance(typeof(List<>).MakeGenericType(type)) and then dynamically calling the generic method using similar reflective methods.

Solution 5 - C#

List<animalType> animals = 
 creator.CreateAnimals<type>(5);

In the above line from your example, animalType and type are run time variables, not types, so this is of course nonsense. A generic version only makes sense, if you know the types at compile time, e.g.:

List<Animal> animals = 
  creator.CreateAnimals<Cow>(5);

where you'd have to constraint types accordingly. If the types are not known, you have to rely completely on reflection...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncmptrerView Question on Stackoverflow
Solution 1 - C#Kirk WollView Answer on Stackoverflow
Solution 2 - C#Jon SkeetView Answer on Stackoverflow
Solution 3 - C#SamuelView Answer on Stackoverflow
Solution 4 - C#KeithSView Answer on Stackoverflow
Solution 5 - C#Paul MichalikView Answer on Stackoverflow