使用 lambda 表达式参数调用泛型方法的反射

2024-01-10

我正在寻找一种使用 lambda 表达式调用通用方法的方法,该表达式在项目数组中调用 Contains。

在本例中,我使用实体框架Where方法,但该场景可以应用于其他IEnumerables。

我需要通过 Reflection 调用上面代码的最后一行,因此我可以使用任何类型和任何属性来传递给 Contains 方法。

var context = new TestEntities();

var items = new[] {100, 200, 400, 777}; //IN list (will be tested through Contains)
var type = typeof(MyType); 

context.Set(type).Where(e => items.Contains(e.Id)); //**What is equivalent to this line using Reflection?**

在研究中,我注意到我应该使用 GetMethod、MakeGenericType 和 Expression 来实现这一点,但我不知道如何做到这一点。拥有这个示例将非常有帮助,这样我就可以了解反射如何与 Lambda 和通用概念一起工作。

基本上,目标是编写一个正确版本的函数,如下所示:

//Return all items from a IEnumerable(target) that has at least one matching Property(propertyName) 
//with its value contained in a IEnumerable(possibleValues)
static IEnumerable GetFilteredList(IEnumerable target, string propertyName, IEnumerable searchValues)
{
    return target.Where(t => searchValues.Contains(t.propertyName));
    //Known the following:
    //1) This function intentionally can't be compiled
    //2) Where function can't be called directly from an untyped IEnumerable
    //3) t is not actually recognized as a Type, so I can't access its property 
    //4) The property "propertyName" in t should be accessed via Linq.Expressions or Reflection
    //5) Contains function can't be called directly from an untyped IEnumerable
}

//Testing environment
static void Main()
{
    var listOfPerson = new List<Person> { new Person {Id = 3}, new Person {Id = 1}, new Person {Id = 5} };
    var searchIds = new int[] { 1, 2, 3, 4 };

    //Requirement: The function must not be generic like GetFilteredList<Person> or have the target parameter IEnumerable<Person>
    //because the I need to pass different IEnumerable types, not known in compile-time
    var searchResult = GetFilteredList(listOfPerson, "Id", searchIds);

    foreach (var person in searchResult)
        Console.Write(" Found {0}", ((Person) person).Id);

    //Should output Found 3 Found 1
}

我不确定其他问题是否解决了这种情况,因为我认为我无法清楚地理解表达式是如何工作的。

Update:

我无法使用泛型,因为我只有在运行时要测试的类型和属性(在 Contains 中)。在第一个代码示例中,假设“MyType”在编译时未知。在第二个代码示例中,类型可以作为参数传递给 GetFilteredList 函数,也可以通过反射 (GetGenericArguments) 获取。

Thanks,


经过对表达式的广泛研究和大量研究后,我可以自己编写一个解决方案。它当然可以改进,但完全符合我的要求。希望它可以帮助别人。

//Return all items from a IEnumerable(target) that has at least one matching Property(propertyName) 
//with its value contained in a IEnumerable(possibleValues)
static IEnumerable GetFilteredList(IEnumerable target, string propertyName, IEnumerable searchValues)
{
    //Get target's T 
    var targetType = target.GetType().GetGenericArguments().FirstOrDefault();
    if (targetType == null)
        throw new ArgumentException("Should be IEnumerable<T>", "target");

    //Get searchValues's T
    var searchValuesType = searchValues.GetType().GetGenericArguments().FirstOrDefault();
    if (searchValuesType == null)
        throw new ArgumentException("Should be IEnumerable<T>", "searchValues");

    //Create a p parameter with the type T of the items in the -> target IEnumerable<T>
    var containsLambdaParameter = Expression.Parameter(targetType, "p");

    //Create a property accessor using the property name -> p.#propertyName#
    var property = Expression.Property(containsLambdaParameter, targetType, propertyName);

    //Create a constant with the -> IEnumerable<T> searchValues
    var searchValuesAsConstant = Expression.Constant(searchValues, searchValues.GetType());

    //Create a method call -> searchValues.Contains(p.Id)
    var containsBody = Expression.Call(typeof(Enumerable), "Contains", new[] { searchValuesType }, searchValuesAsConstant, property);

    //Create a lambda expression with the parameter p -> p => searchValues.Contains(p.Id)
    var containsLambda = Expression.Lambda(containsBody, containsLambdaParameter);

    //Create a constant with the -> IEnumerable<T> target
    var targetAsConstant = Expression.Constant(target, target.GetType());

    //Where(p => searchValues.Contains(p.Id))
    var whereBody = Expression.Call(typeof(Enumerable), "Where", new[] { targetType }, targetAsConstant, containsLambda);

    //target.Where(p => searchValues.Contains(p.Id))
    var whereLambda = Expression.Lambda<Func<IEnumerable>>(whereBody).Compile();

    return whereLambda.Invoke();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 lambda 表达式参数调用泛型方法的反射 的相关文章

  • AWS AppSync 授权

    我计划使用 AWS Appsync 迁移 lambda 函数中的 graphQL 终端节点 该函数由 API 网关的 POST 触发 我研究 AppSync 主要是因为订阅 我无法使用 Lambda 函数创建订阅 我的身份验证机制基于 Au
  • Windows 窗体中的切换开关控件

    我正在设计一个拨动开关控制使用CheckBox 但目前我的控件只能画一个圆圈 如何绘制如下图所示的圆形形状 以及如何根据控件的值更改圆圈的位置以表示选中和未选中的状态 如下图所示 这是我的代码 public class MyCheckBox
  • 使用 c11 标准和 clang 来使用 strcpy_s

    我正在运行 OS X Sierra 并尝试编译一个使用的 c 程序strcpy s 但是我安装的 clang 编译器使用的是 c99 标准 但是据我读到的 https embeddedgurus com barr code 2017 08
  • 不要覆盖 Azure Blob 存储

    我有一种将文件添加到 Azure Blob 存储的方法 问题是我试图指定一个条件 在该条件下它不会覆盖 blob 而只是添加到其中 我正在尝试使用参数访问条件 但是 VS 说这个方法不能采用两个参数 async void archiveNe
  • 使用 Rhino Mocks 模拟集合

    所以我猜这是很多人想做的事情 模拟集合 过去我用 Rhino 做过这样的事情 var col mock MockRepository GenerateMock
  • 使用标准范围连接带有分隔符的字符串范围

    我想使用范围将跨度中包含的四个字节转换为字符串 这是输入和输出的示例 std span
  • 如何转换 UTF-8 <-> UTF16 可移植

    有没有一种简单 可移植的方法 至少是 win32 linux 将 UTF 16 转换为 UTF 8 并返回 最好使用升压 谢谢你的帮助 托比亚斯 Both libiconv http www gnu org software libicon
  • autofac 中的条件组件注册

    是否可以根据其他组件的状态有条件地注册组件 就像是 ContainerBuilder RegisterConditionally
  • Magento SOAP V2 API - 附加属性设置为空

    几个小时以来 我一直在尝试通过 SOAP V2 API 创建具有附加属性的产品 每当我打电话时就会添加该产品目录产品创建但我随请求发送的附加属性被设置为空 每当我不添加附加属性时 这两个属性都会设置为其默认值 因此我认为这些属性正在发送和接
  • 检查字符串中是否存在所有字符值

    我目前正在做这项任务 但我被困住了 目标是读取文件并查找文件中的字符串中是否存在这些字符值 我必须将文件中的字符串与作为参数放入的另一个字符串进行比较 但是 只要每个字符值位于文件中的字符串中 那么它就 匹配 示例 输入和输出 a out
  • 如何声明返回相同类型的 Func Delegate 的 Func Delegate?

    我想编写一个方法 该方法可以完成一些工作 并最终返回另一个与原始方法具有相同签名的方法 这个想法是根据前一个字节值顺序处理字节流 而不进行递归 通过这样调用它 MyDelegate executeMethod handleFirstByte
  • 将 jstring 转换为 QString

    我正在调用一个返回字符串的 Java 函数 QAndroidJniObject obj QAndroidJniObject callStaticObjectMethod
  • IOS Box2D - 身体遵循基于速度波动的点数组的特定路径

    我有一个关于身体的问题 它遵循特定的路径 首先是将身体移动到目标点的方法 const float destinationControl 0 3f b2Vec2 targetPosition path counter b2Vec2 missi
  • 在另一个类中使用一个类对象?

    我正在用 c 制作应用程序 在该应用程序中 我有一个类DataCapture cs 在同一个应用程序中 我有另一个类Listner cs 在 Listner cs 类中 我想使用以下对象DataCapture cs不创建新对象DataCap
  • 通用 lambda 的数量

    可以通过访问非泛型 lambda 的数量来推断其数量operator template
  • 在 C++ 中运行 python [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我有一个用 C 编写的应用程序和一个测试系统 也是用 C 编写的 测试系统非常复杂并且很难改变 我只想做一些小的改变 我的班级是这样的
  • 如何获取 EF 中的实体更改增量?

    我只需要获取已更改字段的列表 数据存储区是 ssce 因此没有可用的触发器 EF 是否支持获取列表或构建通用组件 根据上下文的类型和生成的实体 您可以通过多种不同的方式来完成此操作 如果对象继承自 Entity 或 POCO 您可以使用Ob
  • GCC编译非常慢(文件大)

    我正在尝试编译一个大的 C 文件 专门用于 MATLAB mexing C 文件大约 20 MB 可用来自 GCC 错误跟踪器 https gcc gnu org bugzilla attachment cgi id 36632如果你想玩一
  • 偏专业化朋友声明

    在下面的代码中 template
  • 布尔实现的atomicCAS

    我想弄清楚是否存在错误答案 https stackoverflow com a 57444538 11248508 现已删除 关于Cuda like的实现atomicCAS for bool是 答案中的代码 重新格式化 static inl

随机推荐