我有一個項目在MVC C#code-first。我想將一個類型傳遞給我的方法。我的方法需要一個班才能工作。但我不想通過一門課 - 我希望它是動態的。有沒有可能?C#類型到泛型類
這是我的代碼;
string item ="PERSON" // item is a parametric I give the table name to get the type of table
Assembly asm = Assembly.Load("ProjectName");
Type entityType = asm.GetType(String.Format("NameSpace.Model.{0}", item));
var test = LINQDynamic<>.GetQueryResult(ssa,entityType,ddd); // In LINQDynamic<> I want to use entityType like that LINQDynamic<entityType> but it's not acceptable what can I make the convert type to generic?..Because table name is parametric.Is it possible to convert that type?
public class LINQDynamic<TEntity> where TEntity: class
{
public static object GetQueryResult(object pkKey,Type type, params object[] pkKeys)
{
//TODO
}
}
的可能的複製[如何使用反射來調用泛型方法?(http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method) – kai
是的,可能是一樣的。但我的表名是動態的。我怎樣才能轉換或如何使用反射來創建字符串表名稱到真正的類傳遞給LINQDynamic <這裏是我想要動態類>。 –