C# 传递函数<;T、 布尔>;作为使用反射的参数?

C# 传递函数<;T、 布尔>;作为使用反射的参数?,c#,.net,generics,reflection,C#,.net,Generics,Reflection,我有一个方法: public bool DoStuff<T>(T obj) { // Do Stuff return result; } 我不明白的是如何将对DoStuff的引用封装为Func,在编译时我不知道它,但在运行时知道它,然后将它填充到对象[]中,作为方法调用的参数提供 如果有帮助的话,我正在写一个简单语言的解释器。DoStuff将用简单语言解释lambda表达式,调用的方法将是解释器使用者提供的.NET函数 更新 在遵循Hans评论中提供的链接(谢谢!)之后,我

我有一个方法:

public bool DoStuff<T>(T obj) {
  // Do Stuff
  return result;
}
我不明白的是如何将对DoStuff的引用封装为Func,在编译时我不知道它,但在运行时知道它,然后将它填充到对象[]中,作为方法调用的参数提供

如果有帮助的话,我正在写一个简单语言的解释器。DoStuff将用简单语言解释lambda表达式,调用的方法将是解释器使用者提供的.NET函数

更新

在遵循Hans评论中提供的链接(谢谢!)之后,我实现了以下内容:

Type delegateType = typeof(Func<,>).MakeGenericType(new []{argType, typeof(bool)});
MethodInfo delegatedMethod = this.GetType().GetMethod(nameof(DoStuff), BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance);
MethodInfo generic = delegatedMethod.MakeGenericMethod(argType);

Delegate myDelegate = Delegate.CreateDelegate(delegateType, this, generic);
var args = new object[] { myDelegate };

return instance.GetType().InvokeMember(method.Name, BindingFlags.InvokeMethod, null, instance, args);
它是args数组中唯一的元素,我调用的方法签名是:

public int Any(Func<int, bool> arg)
public int Any(Func arg)

instance是包含Any方法的类的实例,method是Any方法的MethodInfo。

下面是一些有效的代码:

public class AnyImplementer
{
   public int Any(Func<int, bool> func)
    {
        return func(10)? 1: 0;
    }

  
}

public class DoStuffer
{
    public bool DoStuff<T>(T obj)
    {
        return obj.ToString() != string.Empty;
    }

    public int a(Type argType, AnyImplementer anyImplementer)
    {
        Type delegateType = typeof(Func<,>).MakeGenericType(new[] { argType, typeof(bool) });
        MethodInfo delegatedMethod = this.GetType().GetMethod(nameof(DoStuff), BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance);
        MethodInfo generic = delegatedMethod.MakeGenericMethod(argType);

        Delegate myDelegate = Delegate.CreateDelegate(delegateType, this, generic);
        var args = new object[] { myDelegate };

        return (int)anyImplementer.GetType().InvokeMember("Any", BindingFlags.InvokeMethod, null, anyImplementer, args);
    }
}


public class Program
{
   
    public static void Main(string[] args)
    {
        DoStuffer DoStuffer = new DoStuffer();
        AnyImplementer anyImplementer = new AnyImplementer();
       Console.WriteLine(DoStuffer.a(typeof(int), anyImplementer));
       
    }

    
}
公共类实现者
{
公共int Any(Func Func)
{
返回函数(10)?1:0;
}
}
公共级卷扬机
{
公共厕所(T obj)
{
返回obj.ToString()!=string.Empty;
}
公共int a(类型argType,AnyImplementer AnyImplementer)
{
Type delegateType=typeof(Func).MakeGenericType(new[]{argType,typeof(bool)});
MethodInfo delegatedMethod=this.GetType().GetMethod(nameof(DoStuff),BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance);
MethodInfo generic=delegatedMethod.MakeGenericMethod(argType);
Delegate myDelegate=Delegate.CreateDelegate(delegateType,this,generic);
var args=新对象[]{myDelegate};
返回(int)anyImplementer.GetType().InvokeMember(“Any”,BindingFlags.InvokeMethod,null,anyImplementer,args);
}
}
公共课程
{
公共静态void Main(字符串[]args)
{
DoStuffer DoStuffer=新DoStuffer();
AnyImplementer AnyImplementer=新的AnyImplementer();
Console.WriteLine(DoStuffer.a(typeof(int),anyImplementer));
}
}
也可访问 -
其思想是(基于OP的更新和相关问题)使用从泛型方法组的方法信息构造的委托创建并将方法实例包装到对象中。

好吧,泛型不是为这个目的而设计的。我想,你可能想用?@littlecharva来寻找解决方案。你能验证一下这是一个可行的例子吗?Lause是的,就是这样。我创建了一个新项目并粘贴了您的代码,效果非常好。然后,我开始用我的代码替换您的代码,并发现了问题:在我的代码中,我将Func作为argType传递,而我本应传递int。它现在可以完美地工作了!你想发布一个链接到你的粘贴箱作为答案,我会接受它。非常感谢你帮我找到我的错误!
Type delegateType = typeof(Func<,>).MakeGenericType(new []{argType, typeof(bool)});
MethodInfo delegatedMethod = this.GetType().GetMethod(nameof(DoStuff), BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance);
MethodInfo generic = delegatedMethod.MakeGenericMethod(argType);

Delegate myDelegate = Delegate.CreateDelegate(delegateType, this, generic);
var args = new object[] { myDelegate };

return instance.GetType().InvokeMember(method.Name, BindingFlags.InvokeMethod, null, instance, args);
myDelegate = {Method = {Boolean DoStuff[Func`2](System.Func`2[System.Int32,System.Boolean])} 
public int Any(Func<int, bool> arg)
public class AnyImplementer
{
   public int Any(Func<int, bool> func)
    {
        return func(10)? 1: 0;
    }

  
}

public class DoStuffer
{
    public bool DoStuff<T>(T obj)
    {
        return obj.ToString() != string.Empty;
    }

    public int a(Type argType, AnyImplementer anyImplementer)
    {
        Type delegateType = typeof(Func<,>).MakeGenericType(new[] { argType, typeof(bool) });
        MethodInfo delegatedMethod = this.GetType().GetMethod(nameof(DoStuff), BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance);
        MethodInfo generic = delegatedMethod.MakeGenericMethod(argType);

        Delegate myDelegate = Delegate.CreateDelegate(delegateType, this, generic);
        var args = new object[] { myDelegate };

        return (int)anyImplementer.GetType().InvokeMember("Any", BindingFlags.InvokeMethod, null, anyImplementer, args);
    }
}


public class Program
{
   
    public static void Main(string[] args)
    {
        DoStuffer DoStuffer = new DoStuffer();
        AnyImplementer anyImplementer = new AnyImplementer();
       Console.WriteLine(DoStuffer.a(typeof(int), anyImplementer));
       
    }

    
}