如何传递参数数量与原始设计不同的委托 [英] How to pass a delegate with different number of arguments than originally designed

查看:228
本文介绍了如何传递参数数量与原始设计不同的委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:


  1. 我设计了一个使用通用委托函数(Func <.. 。>)来计算两个实数向量之间的距离

  1. I have designed a framework that uses a generic delegate function (Func<...>) to calculate distance between two real number vectors

该代表具有以下形式:

Func<double[], double[], double> distance;


  • 我实现了许多不同的距离函数

  • I have many different distance functions implemented

    我宁愿不更改距离代表的签名

    I would rather not change the signature of distance delegate

    我需要创建一个新函数,除了两个双矢量之外,还需要一些其他功能参数,即

    I need to create a new function that besides two double vectors needs some additional parameters i.e.

    Func<double[], double[], double, double, double> newDistance;
    


  • 这不必是委托。我想要的是定义一个带有附加参数的方法,并将其传递给旧接口。

  • This does not have to be the delegate. What I want is to define a method with additional parameters and pass it to the old interface.

    这可以以及如何实现,无需进行认真的重新设计和更改距离代表的签名就可以实现?

    can this and how, be achieved without a serious redesign and without changing the signature of distance delegate?

    问候,
    塞巴斯蒂安

    Regards, Sebastian

    推荐答案

    好的,我想我已经找到了一种方法:

    OK I think I have found a way:

    我可以创建一个使用这两个附加参数并返回的方法Func代表。

    I can create a method that takes this two additional parameters and returns a Func delegate. Something like this

    public static Func<double[], double[], double> NewDistanceMethodWithParams(double alpha, double beta)
    {                       
        Func<double[], double[], double> dist = (vec1, vec2) =>
        {
             //do some calculations on vec1 and vec2 with alpha, beta params                     
        };
    
        return dist;
    }
    

    第一次测试表明它可以正常工作! :)

    First tests shows that it works !!! :)

    这篇关于如何传递参数数量与原始设计不同的委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆