我在实体框架上下文中有一个Employees DbSet,可以查询为:
IQueryable employees = _context.Employees;
想法是使用反射执行以下方法:
var result= _context.Employees.OfType<PaidEmployee>()
我扩展了Employee对象来创建PaidEmployee类。我想使用REFLECTION查询PaidEmployee的上下文。
Assembly asm = Assembly.LoadFrom("MyModel.dll");
Type t = asm.GetType("PaidEmployee");
var ofType = typeof(Queryable).GetMethod("OfType",
BindingFlags.Static | BindingFlags.Public);
var methodinfo = ofType.MakeGenericMethod(t);
var obj = methodinfo.Invoke(employees , null);
当我执行上面的代码时,它给了我错误:
用户代码HResult = -2147352562未处理System.Reflection.TargetParameterCountException消息=参数计数不匹配。
Source = mscorlib StackTrace:位于System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(对象obj,BindingFlags invokeAttr,活页夹活页夹,Object []参数,CultureInfo文化),位于System.Reflection.RuntimeMethodInfo.Invoke(对象obj,BindingFlags invokeAttr,活页夹活页夹,对象[]参数,在System.Reflection.MethodBase.Invoke中的CultureInfo文化(Object obj,Object []参数)在e:\ Projects \ Tests \ test_dynamic.cs:line 54中的Tests.test_dynamic.TestMethod2()处
尝试
var obj = methodinfo.Invoke(null, new[] { employees });
该OfType
是静态的,所以null
obj
(的第一个参数Invoke
,即对象的实例来使用的方法)!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句