运行时在C#mongodb中传递类名
我正在尝试传递我的类名运行时,如下所示运行时在C#mongodb中传递类名,c#,mongodb,C#,Mongodb,我正在尝试传递我的类名运行时,如下所示 Assembly a = Assembly.Load("Test.Model"); Type el = a.GetType("Test.Model.ClassName"); var myObj = Activator.CreateInstance(el); var ab = db.GetCollection<Here I Want
Assembly a = Assembly.Load("Test.Model");
Type el = a.GetType("Test.Model.ClassName");
var myObj = Activator.CreateInstance(el);
var ab = db.GetCollection<Here I Want to pass class name>(Properties["ClassName"]);
Assembly a=Assembly.Load(“Test.Model”);
类型el=a.GetType(“Test.Model.ClassName”);
var myObj=Activator.CreateInstance(el);
var ab=db.GetCollection(属性[“ClassName”]);
类名将在运行时传递。
我怎样才能做到这一点
因此,如果我的类名是ABC,代码将如下
var ab = db.GetCollection<ABC>("ABC");
var ab=db.GetCollection(“ABC”);
您可以将反射与
var methodInfo = db.GetType().GetMethod("GetCollection");
methodInfo = methodInfo.MakeGenericMethod(/*The type to put in*/);
methodInfo.Invoke(db, new object[] { Properties["ClassName"] });
这有用吗