Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to do a generic persist method using reflecion and MongoDB.

The normal code is:
C#
public bool InsertParent(ParentDTO parent)
       {
           var collection = db.GetCollection<Parent>(ParentCollectionName);
           var collectionC = db.GetCollection<Child>(ChildCollectionName);
           try
           {
               var parentdb = new Parent();
               parentdb.ParentCode = parent.ParentCode;

               collection.InsertOne(parentdb);
               parent.ListChild.ForEach(c => {
                   var childDB = new Child();
                   childDB.ParentID = parentdb.ParentID;
                   childDB.ChildCode = c.ChildCode;
                   collectionC.InsertOne(childDB);
               }
                 );
               return true;
           }
           catch (System.Exception)
           {
               throw;
           }
       }

But in this way, I always must create the collections with the right type:
C#
var collection = db.GetCollection<parent>(ParentCollectionName);

and I´d like to do something like that:
C#
public bool InsertMongoDb(object objeto)
        {
              if (objeto == null)
              {
                  return false;
              }
              var typeO = objeto.GetType();
              
              //here is the problem. The var typeO is not accepted. 
               var collection = db.GetCollection<???>(objeto.collectionName);             

              PropertyInfo[] properties = typeO.GetProperties();
              foreach (var ppo in properties)
              {
                  if (ppo.PropertyType.Name.ToLower() == "List`1".ToLower() )
                    {
                        Type type = ppo.PropertyType;
                        // here I will read the CHILD objects
                        if (IsList(type))
                        {
                           var ret = GetCollectionElementType(type);
                            PropertyInfo info = typeO.GetProperty(ppo.Name);
                            var list = (System.Collections.IList)info.GetValue(objeto, null);
                            foreach (var item in list) 
                            {
                                PropertyInfo[] propertieS = ret.GetProperties();
                                foreach (var ppi in propertieS)
                                {
                                  //Here I will call recusrivelly
                                  InsertMongoDb(?????)  
                                  //How can I get the object here.
                                }                                
                            }
                        }
                    }
              }

            return true;
        }


Thanks!

What I have tried:

I tried:
C#
objeto.GetType().BaseType;
Posted
Updated 24-Oct-23 11:04am
v2
Comments
[no name] 19-Jul-21 12:18pm    
public bool InsertMongoDb<t>(T objeto) { etc. }

1 solution

Hi,

I know it's a bit late to answer this question, but you could use reflection to construct the generic method with something similar to this:
C#
//var typeO = objeto.GetType();
//here is the problem. The var typeO is not accepted. 
//var collection = db.GetCollection<???>(objeto.collectionName);

// get `db` type because we need the method information from the type
var dbType = db.GetType();

// get generic method `GetCollection<T>`
var genericMethod = dbType.GetMethod("GetCollection");

// construct the method using `typeO` for T like `GetCollection<typeO>`
var methodInfo = mi.MakeGenericMethod(typeO);

// prepare the `objeto` arguments
object[] args = { objeto };

// call the method using instance `db` and argument `objeto`
var collection = methodInfo.Invoke(db, args);



And then, instead of the following code
C#
var list = (System.Collections.IList)info.GetValue(objeto, null);
foreach (var item in list) 
{
PropertyInfo[] propertieS = ret.GetProperties();
foreach (var ppi in propertieS)
{
//Here I will call recusrivelly
InsertMongoDb(?????)  
//How can I get the object here.
}
}


you can use:
C#
var list = (System.Collections.IList)info.GetValue(objeto, null);
foreach (var item in list)
InsertMongoDb(item);


Good luck and have a nice day!
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900