如何将通用参数传递给多个委托。 2 个具有通用参数的代表

发布于 2024-12-07 07:43:39 字数 1452 浏览 0 评论 0原文

我有一个接受两个委托作为参数的方法。

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 delegates 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 delegates, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

衣神在巴黎 2024-12-14 07:43:39

您需要使用下一个签名创建一个委托:

delegate void MyCustomDelegate (params object[] params);

然后将 Method2Delegate 和 Method3Delegate 定义为 MyCustomDelegate 类型。

You need to create a delegate with the next signature:

delegate void MyCustomDelegate (params object[] params);

And then to define Method2Delegate and Method3Delegate as MyCustomDelegate type.

聽兲甴掵 2024-12-14 07:43:39

这样做怎么样:

method1(Func<object[],string> delegate1,Func<object[], object> delegate2,params Object[] parameters)
{

// There is lot of other code, I haven't put here. To make it clear.

    string key = delegate1(parameters);

    object instance = delegate2(parameters); 
// Getting errors here, as parameters mismatch. 

}

在调用者处,这样

Method1(Method2,Method3,string str1,string str2,object object1,Object object2,Object object3);

您就不需要定义动态调用的委托。

what about doing it like this:

method1(Func<object[],string> delegate1,Func<object[], object> delegate2,params Object[] parameters)
{

// There is lot of other code, I haven't put here. To make it clear.

    string key = delegate1(parameters);

    object instance = delegate2(parameters); 
// Getting errors here, as parameters mismatch. 

}

and at the caller just

Method1(Method2,Method3,string str1,string str2,object object1,Object object2,Object object3);

this way you don't need to define delegates that dynamically invoked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文