C# 类型到泛型 class
C# Type to Generic class
我有一个 MVC C# 代码优先项目。我想将类型传递给我的方法。我的方法需要 class 才能工作。但我不想传递 class - 我希望它是动态的。可不可以?
这是我的代码;
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
}
}
反射示例:
public static void Main(string[] args)
{
Type t = typeof(string);
Type fooType = typeof(Foo<>);
Type genericFooType = fooType.MakeGenericType(t);
object o = Activator.CreateInstance(genericFooType,null);
MethodInfo method = genericFooType.GetMethod("Bar");
method.Invoke(o, null);
}
public class Foo<T> where T:class
{
public void Bar()
{
Console.WriteLine ("Calling Bar");
}
}
您似乎没有在 LINQDynamic
class 中使用 TEntity
- 而是将类型作为参数传递。这使得 TEntity
变得毫无意义。
尝试:
public class LINQDynamic
{
public static object GetQueryResult(object pkKey,Type type, params object[] pkKeys)
{
//TODO
}
}
var test = LINQDynamic.GetQueryResult(ssa,entityType,ddd);
我以前 运行 遇到过同样的问题。这通常是我所做的。继续沿着代码中的调用堆栈前进,直到到达可以描述接口而不是通用接口发生了什么的地步,即; IEnumerable GetResults(字符串项).
然后,使用表达式树对方法的内容进行代码生成(这允许您保留通用方法调用),并将编译后的 lambda 存储在静态字典中,其中键是您的 "item" 和值为 Func < IEnumerable < IEntity >>。
但要小心行事。 运行 很容易陷入多线程、内存泄漏和其他并发和长期 运行ning 应用程序(如 Web 应用程序)中令人讨厌的问题。我几乎会建议你不要这样做。
采纳了的建议:
object[] newobj = { pkey,entityType,pKyes };
object[] parameters = new object[] { newobj };
Type t = typeof (string);
Type linqType = typeof (LinqDynamic<>);
Type genericLinqType = linqType.MakeGenericType(entityType);
object o = Activator.CreateInstance(genericLinqType, null);
MethodInfo method = genericLinqType.GetMethod("GetEntityByPrimaryKey");
var results = method.Invoke(o, parameters);
我有一个 MVC C# 代码优先项目。我想将类型传递给我的方法。我的方法需要 class 才能工作。但我不想传递 class - 我希望它是动态的。可不可以?
这是我的代码;
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
}
}
反射示例:
public static void Main(string[] args)
{
Type t = typeof(string);
Type fooType = typeof(Foo<>);
Type genericFooType = fooType.MakeGenericType(t);
object o = Activator.CreateInstance(genericFooType,null);
MethodInfo method = genericFooType.GetMethod("Bar");
method.Invoke(o, null);
}
public class Foo<T> where T:class
{
public void Bar()
{
Console.WriteLine ("Calling Bar");
}
}
您似乎没有在 LINQDynamic
class 中使用 TEntity
- 而是将类型作为参数传递。这使得 TEntity
变得毫无意义。
尝试:
public class LINQDynamic
{
public static object GetQueryResult(object pkKey,Type type, params object[] pkKeys)
{
//TODO
}
}
var test = LINQDynamic.GetQueryResult(ssa,entityType,ddd);
我以前 运行 遇到过同样的问题。这通常是我所做的。继续沿着代码中的调用堆栈前进,直到到达可以描述接口而不是通用接口发生了什么的地步,即; IEnumerable GetResults(字符串项).
然后,使用表达式树对方法的内容进行代码生成(这允许您保留通用方法调用),并将编译后的 lambda 存储在静态字典中,其中键是您的 "item" 和值为 Func < IEnumerable < IEntity >>。
但要小心行事。 运行 很容易陷入多线程、内存泄漏和其他并发和长期 运行ning 应用程序(如 Web 应用程序)中令人讨厌的问题。我几乎会建议你不要这样做。
采纳了
object[] newobj = { pkey,entityType,pKyes };
object[] parameters = new object[] { newobj };
Type t = typeof (string);
Type linqType = typeof (LinqDynamic<>);
Type genericLinqType = linqType.MakeGenericType(entityType);
object o = Activator.CreateInstance(genericLinqType, null);
MethodInfo method = genericLinqType.GetMethod("GetEntityByPrimaryKey");
var results = method.Invoke(o, parameters);