I'm trying to pass in a dynamic model class to a more generic class. Part of the generic class is using dapper querying to a typed List. I'm trying to make this generic class modular in that in can take in any model class and be able to use this model class as an output of the query.
Am I even approaching this problem in the right way?
So in the example below, I'd like for the GetFruits method to be dynamic in that it would return List<Bananas> if Bananas is passed in or List<Orange> if Oranges is passed in
class FruitBasket
{
public FruitBasket(dynamic ModelClass)
{
}
public List<Orange> GetFruits(connection)
{
List<Orange> oranges = connection.Query<Orange>("Select * from Oranges");
return oranges
}
}
//Passes in Oranges model class
var fruits = new FruitBasket(new Oranges())
//Passes in Bananas model class
var fruits = new FruitBasket(new Bananas ())
var fruits- it is generally hard to declare variable of type only known at compile time - so depending on what you hope to achieve existing answers my or may not help... ("hard" as in "object/dynamicis your only option and you don't get any type safety")