我正在尝试使用Action<string, bool>
类型的委托来调用Dispatcher.Invoke
与splgate一起使用的命名方法
private void SomeMethod(string name,out bool result)
{
...
}
当我使用以下内容时,它会显示错误,表示与签名不匹配。
Dispatcher.Invoke(new Action<string, bool>(SomeMethod),new Object[2]{name, result});
我在这里做错了什么。请纠正我。
答案 0 :(得分:2)
Action<,>
没有out
参数。您需要使用自己的代理人,如下所示:
public void ActionOut<T1, T2>(T1 input, out T2 output)
可能工作(就不抛出异常而言) - 我确信它会反射;我对Dispatcher.Invoke
不太确定。 不会将结果值保留在result
变量中 - 它会将其保留在数组中,然后您将忽略它。你想要:
object[] args = new object[] { name, null };
Dispatcher.Invoke(new ActionOut<string, bool>(SomeMethod), args);
result = (bool) args[1];
但最好只让方法返回结果,然后使用Func<string, bool>
代替。您应该从不在返回out
的方法中使用void
参数。在我看来,out
参数被有效地设计为允许您返回多个值 - 如果您只想返回一个值,请使用返回类型!