如何将通用参数传递给多个委托。 2 个具有通用参数的代表
我有一个接受两个委托作为参数的方法。
method1(Delegate delegate1,Delegate delegate2,params Object[] parameters)
{
// There is lot of other code, I haven't put here. To make it clear.
string key = delegate1.DynamicInvoke(parameters);
object instance = delegate2.DynamicInvoke(parameters);
// Getting errors here, as parameters mismatch.
}
// Code from the Calling class
// There are many other classes in my project, which are calling Method1,
// but the number of parameters of method2 and method3 always vary.
private string Method2(object[] paramsObject)
{
string key = string.Empty;
foreach (object obj in paramsObject)
{
key += obj.ToString() + "|";
}
return key.Trim(new char[]{'|'});
}
private object Method3(object[] paramsObject)
{
object object1 = paramsObject[0];
object object2 = paramsObject[1];
object object3 = paramsObject[2];
object object4 = GetObjectUsingParameters(object1,object2,object3);
return object4;
}
Func<string, string, string> Method2Delegate = Method2;
Func<object1,object2,object3,object4> Method3Delegate = Method3;
//Calling Method1
Method1(Method2Delegate,Method3Delegate,string str1,string str2,object object1,Object object2,Object object3);
调用 delegate
时,我在 Method1
中收到错误,参数不匹配错误。
因为我们最后只能有一个 params 参数作为方法的输入。
您能否告诉我,如何将参数绑定到同一个委托
或者如何解决此问题?
提前致谢。
I have a method which accepts two delegate
s as parameters.
method1(Delegate delegate1,Delegate delegate2,params Object[] parameters)
{
// There is lot of other code, I haven't put here. To make it clear.
string key = delegate1.DynamicInvoke(parameters);
object instance = delegate2.DynamicInvoke(parameters);
// Getting errors here, as parameters mismatch.
}
// Code from the Calling class
// There are many other classes in my project, which are calling Method1,
// but the number of parameters of method2 and method3 always vary.
private string Method2(object[] paramsObject)
{
string key = string.Empty;
foreach (object obj in paramsObject)
{
key += obj.ToString() + "|";
}
return key.Trim(new char[]{'|'});
}
private object Method3(object[] paramsObject)
{
object object1 = paramsObject[0];
object object2 = paramsObject[1];
object object3 = paramsObject[2];
object object4 = GetObjectUsingParameters(object1,object2,object3);
return object4;
}
Func<string, string, string> Method2Delegate = Method2;
Func<object1,object2,object3,object4> Method3Delegate = Method3;
//Calling Method1
Method1(Method2Delegate,Method3Delegate,string str1,string str2,object object1,Object object2,Object object3);
I am getting an error in Method1
when invoking the delegate
s, parameters mismatch error.
As we can have only one one params parameter as input for a method at the end.
Could you please let me know, how can I bind the parameters to the same delegate
or how can I resolve this issue?
Thanks inadvance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用下一个签名创建一个委托:
然后将 Method2Delegate 和 Method3Delegate 定义为 MyCustomDelegate 类型。
You need to create a delegate with the next signature:
And then to define Method2Delegate and Method3Delegate as MyCustomDelegate type.
这样做怎么样:
在调用者处,这样
您就不需要定义动态调用的委托。
what about doing it like this:
and at the caller just
this way you don't need to define delegates that dynamically invoked.