.Net Unable to dynamically create type to pass into MethodName<T>

Viewed 13

I am building a small console app that will get me information out of mongoDb and want it to be as reusable as possible.

I have a method that queries the db and returns data as a type of IMongoQueryable<T> because this is coming from a console application, I want the args to include the class \ type thats going to be passed in.

However in the code below I get error:

CS0118 'typeArgument' is a variable but is used like a type

on code GetDetails<typeArgument>(connectionString, db, collection)

void Main()
{
    var connectionString = "connectionString";
    var db = "dbName";
    var collection = "CollectionName";
    var activityId = 992018;
    string typeName = "Simon.AgentActivityDetails";
    Type typeArgument = Type.GetType(typeName);
    var result = GetDetails<typeArgument>(connectionString, db, collection); //<-- Where clause goes here

}

public IMongoQueryable<T> GetDetails<T>(string connectionString, string db, string collection)
{
    var dbClient = new MongoClient(connectionString);
    IMongoDatabase _mongoDb = dbClient.GetDatabase(db);
    IMongoQueryable<T> result = _mongoDb.GetCollection<T>(collection).AsQueryable();
    return result;
}

First Question, is what I want to do possible? If so, how do I achieve this?

0 Answers
Related