MongoDb, C#, Reflection and dynamic execution.

1,253 views
Skip to first unread message

sunil krishnan

unread,
Sep 20, 2015, 1:25:08 AM9/20/15
to mongodb-csharp

I am trying to build a generic(literally) function to fetch results from collection in MongoDB using c#/.NET driver. As we are aware pretty much everything in the driver is GENERIC. Using Reflections and MakeGenericMethod to achieve these.

So far, I have reached upto executing the the equivalent of MOngoDatabase.GetCollection' . Below is my code... I am stuck at the last n final (perhaps) collection.FindAsync method.

var method_info = typeof(IMongoDatabase).GetMethod("GetCollection" );
var method_ref = method_info.MakeGenericMethod(poco_type);
(poco_type is dynamically looked from a dictionary of collectionnames and associated POCO)

object[] method_arguments = new object[] { collectionName,null };
var  collection = method_ref.Invoke(_db,  method_arguments);

MethodInfo collection_method = collection.GetType().GetMethod("FindAsync");

var collection_method_ref = collection_method.MakeGenericMethod(poco_type);

var filter_obj = new BsonDocument(filter);

method_arguments = new object[] { filter_obj,null,null };

**var result = collection_method_ref.Invoke(collection, method_arguments);**

I get error in the above line of code... Error is

"Object of type 'MongoDB.Bson.BsonDocument' cannot be converted to type
'MongoDB.Driver.FilterDefinition`1[System.RuntimeType]'."

What am I doing wrong ? I cant seem to go further. Is this even possible ?

Help greatly appreciated..

Craig Wilson

unread,
Sep 20, 2015, 10:34:33 AM9/20/15
to mongodb-csharp
User-defined implicit conversions, as those defined by the  driver, are not possible to do with reflection. Rather, you should use BsonDocumentFilterDefinition<> in this case.

Could you explain why you would be doing this? If you really have a dynamic need, then there are 2 better alternatives to using reflection like this: BsonDocument and the dynamic type. 

sunil krishnan

unread,
Sep 20, 2015, 11:59:33 AM9/20/15
to mongodb-csharp
Hello Craig,

The reason I am trying to do this is to primarily have a single gateway for the entire system to get JSON data from the backend for the UI. Rather than writing code for each 'collection' data. so effectively I know the collection name and associated POCO only at runtime . These are looked up by from a dictionary (to avoid switch and IF constructs), ie, UI will pass an 'id' only. Sort of FACTORY Pattern...

Going REFLECTION way, I am stuck at the FilterDefinition..which is a required parameter for pretty much everything to do with FindAsync etc...
AND I cant seem to define the expected type.... Debug info tells me that I seem to be OK till GetCollection method... 

Hope you get my drift...

Craig Wilson

unread,
Sep 20, 2015, 12:32:49 PM9/20/15
to mongodb-csharp

sunil krishnan

unread,
Sep 20, 2015, 12:40:59 PM9/20/15
to mongodb-csharp
Thank you.. will have a look at it.. 


On Sunday, 20 September 2015 10:55:08 UTC+5:30, sunil krishnan wrote:

Rajat Agarwal

unread,
Dec 9, 2017, 1:47:23 AM12/9/17
to mongodb-csharp
Hello Sunil,

I cam across your post while searching for a similar implementation. I am trying to develop a self service BI on mongoDB. Will you be able to share the code or provide required directions. I am fairly new to Mongo DB so do not have much idea as to where to start. Having the code will help me a lot

Regards
Rajat
Reply all
Reply to author
Forward
0 new messages