0

I have a generic method. I do not know what the type will be at the time of writing this function. It will change based on application which is why I need it to return generic. I would just return a json string, however I am using petapocos which has a fetch method and I have to specify type in order to get anything from the database.

For Example

C# method

public IEnumerable<T> GetItems<T>(string type)
{
    return DatabaseContext.Database.Fetch<T>("SELECT * FROM " + type)
}

Any Idea how I can get the ajax method to send over a specific type?

Currently I am sending type over as a string which works for the SQL query, but won't allow me to specify type for the Fetch method.

Taylor Mitchell
  • 614
  • 11
  • 27
  • 1
    You will need to use Reflection and [MakeGenericMethod](http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod.aspx) as shown in [this answer](http://stackoverflow.com/a/232621/43846) – stuartd Oct 12 '15 at 15:50

1 Answers1

0

So the best way I've found to do this seems to return a generic dynamic object. I'm using it on the javascript side anyways and the model type is already known at this point

public IEnumerable<object> GetItems(string type)
{
    return DatabaseContext.Database.Fetch<object>("SELECT * FROM " + type)
}
Taylor Mitchell
  • 614
  • 11
  • 27