如何通过使用lambda表达式作为参数进行反射来调用方法? [英] How do I invoke a method through reflection with a lambda expression as a parameter?

查看:181
本文介绍了如何通过使用lambda表达式作为参数进行反射来调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  MethodInfo m = myList.GetType()。GetMethod(ConvertAll,System。 Reflection.BindingFlags.InvokeMethod).MakeGenericMethod(typeof运算(目标)); 
列表< object> myConvertedList =(List< object>)m.Invoke(myList,new object [] {(t =>(object)t)});

myList是特定类型(应用程序未知)的通用列表,我想转换它到一个对象列表来做一些操作。



然而,这个失败与此错误:不能将lambda表达式转换为键入'object',因为它不是委托类型



你能帮我找到问题吗?我试图做一些不可能的事情吗?

是否有其他方法可以实现同样的目的?

解决方案

lambda表达式可以转换为具有正确签名的委托类型或表达式树 - 但您需要指定它是哪种委托类型。



我认为你的代码会更简单,如果你把它作为一个通用的方法:

  public static List< object> ConvertToListOfObjects< T>(List< T>列表)
{
return list.ConvertAll< object>(t => t);
}

然后你只需要找到并调用即可方法一般:

$ p $ MethodInfo method = typeof(Foo).GetMethod(ConvertToListOfObjects,
BindingFlags.Static | BindingFlags 。上市);
类型listType = list.GetType()。GetGenericArguments()[0];
MethodInfo concrete = method.MakeGenericMethod(new [] {listType});
列表< object> objectList =(List< object>)concrete.Invoke(null,
new object [] {list});

完整示例:

 使用系统; 
使用System.Reflection;
使用System.Collections.Generic;

class Test
{
public static List< object> ConvertToListOfObjects< T>(List< T>列表)
{
return list.ConvertAll< object>(t => t);
}

static void Main()
{
object list = new List< int> {1,2,3,4};

MethodInfo method = typeof(Test).GetMethod(ConvertToListOfObjects,
BindingFlags.Static | BindingFlags.Public);
类型listType = list.GetType()。GetGenericArguments()[0];
MethodInfo concrete = method.MakeGenericMethod(new [] {listType});
列表< object> objectList =(List< object>)concrete.Invoke(null,
new object [] {list});

foreach(objectList中的对象)
{
Console.WriteLine(o);
}
}
}


I want to do this:

MethodInfo m = myList.GetType().GetMethod("ConvertAll", System.Reflection.BindingFlags.InvokeMethod).MakeGenericMethod(typeof(object));
List<object> myConvertedList = (List<object>)m.Invoke(myList, new object[]{ (t => (object)t)});

myList is a generic list of a specific type (unknown to the application), and I want to convert it to a list of objects to do some operations.

However this fails with this error: "Cannot convert lambda expression to type 'object' because it is not a delegate type"

Can you help me find what's wrong? Am I trying to do something that's not possible?

Is there some other way to achieve the same thing?

解决方案

A lambda expression is convertible to either a delegate type or an expression tree with the right signature - but you need to specify which delegate type it is.

I think your code would be much simpler if you made this a generic method:

public static List<object> ConvertToListOfObjects<T>(List<T> list)
{
    return list.ConvertAll<object>(t => t);
}

Then you just need to find and invoke that method generically:

MethodInfo method = typeof(Foo).GetMethod("ConvertToListOfObjects",
    BindingFlags.Static | BindingFlags.Public);
Type listType = list.GetType().GetGenericArguments()[0];
MethodInfo concrete = method.MakeGenericMethod(new [] { listType });
List<object> objectList = (List<object>) concrete.Invoke(null, 
                                                   new object[]{list});

Complete example:

using System;
using System.Reflection;
using System.Collections.Generic;

class Test
{
    public static List<object> ConvertToListOfObjects<T>(List<T> list)
    {
        return list.ConvertAll<object>(t => t);
    }

    static void Main()
    {
        object list = new List<int> { 1, 2, 3, 4 };

        MethodInfo method = typeof(Test).GetMethod("ConvertToListOfObjects",
            BindingFlags.Static | BindingFlags.Public);
        Type listType = list.GetType().GetGenericArguments()[0];
        MethodInfo concrete = method.MakeGenericMethod(new [] { listType });
        List<object> objectList = (List<object>) concrete.Invoke(null,
                                                    new object[] {list});

        foreach (object o in objectList)
        {
            Console.WriteLine(o);
        }
    }
}

这篇关于如何通过使用lambda表达式作为参数进行反射来调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆