.net c#如何使用委托作为调用参数?

How to use delegate as invoke parameter?
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我如何给动作委托作为参数来调用:

class C {
    static void A {}
    public static void F(Action action) { action(); }
}

现在,我可以直接致电F:

F(A);

但是我该如何使用invoke.以下内容无法编译:

MethodInfo methodInfo = typeof(C).GetMethod("F", BindingFlags.Static |
                                                 BindingFlags.NonPublic);
methodInfo.Invoke(null, new object[]{ A });

我尝试过的其他任何事情都没有:

methodInfo.Invoke(null, new object[]{ C.A });
methodInfo.Invoke(null, new object[]{ () => C.A() });

编译器表示无法将参数转换为"对象".

解决过程1

试试这个

methodInfo.Invoke(null, new object[] { new Action(C.A) });
速聊1:
实际上 new Action(C.A)
速聊2:
是的,如果我们需要不同的班级C.A.谢谢.
速聊3:
不客气.如果此答案帮助您将其标记为答案,那么其他人将从中受益
解决过程2

首先,您的代码有一些问题:

1-C.A必须声明方法public以便在外部使用.

2-BindingFlags.NonPublic是阻止访问方法,'F'因为它是public.使用BindingFlags.Public代替.

现在,您可以执行以下操作:

methodInfo.Invoke(null, new object[] { new Action(() => { C.A(); }) });
速聊1:
您的答案也可以,但是Sriram的解决方案更简单,不需要lambda.
解决过程3

具体而言,要在另一个线程中更新label1,请使用带有Lambda语句的参数和Func所需的参数的Invoke.

    Int32 c = 0;
    Int32 d = 0;
    Func<Int32, Int32,Int32> y;


    c = (Int32)(label1.Invoke(y = (x1, x2) => { label1.Text = (x1 +   
    x2).ToString();  return x1++; }, c,d));
    d++; 

通常,将label1替换为methodInfo.使用Func.使用Action,您无法传递参数.

How do I give an action delegate as a parameter into invoke:

class C {
    static void A {}
    public static void F(Action action) { action(); }
}

Now I can call F directly:

F(A);

But how do I do the same with an invoke. The following does not compile:

MethodInfo methodInfo = typeof(C).GetMethod("F", BindingFlags.Static |
                                                 BindingFlags.NonPublic);
methodInfo.Invoke(null, new object[]{ A });

Neither does anything else I have tried like:

methodInfo.Invoke(null, new object[]{ C.A });
methodInfo.Invoke(null, new object[]{ () => C.A() });

The compiler says it cannot convert the parameter into "object".

Solutions1

Try this

methodInfo.Invoke(null, new object[] { new Action(C.A) });
Talk1:
Actually new Action(C.A)
Talk2:
yes if it were in different class we need C.A. Thanks.
Talk3:
You're welcome. If this answer helped you mark as answer, so that others will be benefited
Solutions2

First of all your code has a few problems:

1- Method C.A should be declared public in order to be used outside.

2- BindingFlags.NonPublic is preventing to access method 'F' since it is public. Use BindingFlags.Public instead.

Now, you can do as:

methodInfo.Invoke(null, new object[] { new Action(() => { C.A(); }) });
Talk1:
Your answer works too but Sriram's solution is more simple, the lambda is not necessary.
Solutions3

Specifically, to update label1 in another thread, use the Invoke with arguments with a Lambda statement and the parameters the Func requires.

    Int32 c = 0;
    Int32 d = 0;
    Func<Int32, Int32,Int32> y;


    c = (Int32)(label1.Invoke(y = (x1, x2) => { label1.Text = (x1 +   
    x2).ToString();  return x1++; }, c,d));
    d++; 

Generically, replace label1 by methodInfo. Use Func. With Action you can't pass arguments.

转载于:https://stackoverflow.com/questions/19081646/how-to-use-delegate-as-invoke-parameter

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入