对IQueryable OfType<>的反思

时间:2015-06-11 08:40:49

标签: c# entity-framework generics reflection

我的Entity Framework上下文中有一个Employees DbSet,可以查询为:

echo "before calling";
require_once BASEPATH.'core/CodeIgniter.php';
echo "after calling";

想法是使用Reflection:

执行以下方法
IQueryable employees = _context.Employees;

我已经扩展了Employee对象以创建一个PaidEmployee类。 我想使用REFLECTION查询PaidEmployee的上下文。

var result= _context.Employees.OfType<PaidEmployee>()

当我执行上面的代码时,它给出了错误:

  

System.Reflection.TargetParameterCountException未被用户处理   代码HResult = -2147352562消息=参数计数不匹配   来源= mscorlib程序   堆栈跟踪:          在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,   BindingFlags invokeAttr,Binder binder,Object []参数,   文化信息文化)          在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,   文化信息文化)          在System.Reflection.MethodBase.Invoke(Object obj,Object []参数)          在e:\ Projects \ Tests \ test_dynamic.cs中的Tests.test_dynamic.TestMethod2()中:第54行InnerException:

1 个答案:

答案 0 :(得分:3)

尝试

var obj = methodinfo.Invoke(null, new[] { employees });

OfType是静态的,因此null objInvoke的第一个参数,即用于该方法的对象的实例)!

相关问题