2017-05-02 11 views
0

ランタイム通過クラス名

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"]); 

は、クラス名は、実行時に渡されます。 これをどうすれば実現できますか?私のクラス名がABCであれば

ので、コードはあなたが

var methodInfo = db.GetType().GetMethod("GetCollection"); 
methodInfo = methodInfo.MakeGenericMethod(/*The type to put in*/); 
methodInfo.Invoke(db, new object[] { Properties["ClassName"] }); 

でリフレクションを使用することができます

var ab = db.GetCollection<ABC>("ABC"); 

答えて

1

のようになります。この支援していますか?