如何将方法作为参数传递? [英] How to pass in a method as a parameter?

查看:344
本文介绍了如何将方法作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到我在ASP.NET应用程序中重复了很多C#代码,因此想创建一个通用方法。我有一系列这样的私有方法:

I've just noticed I'm repeating a lot of C# code in my ASP.NET application so want to create a generic method. I have a series of private methods like this:

private void PopulateMyRepeatedControl()
{
    DBUtil DB = new DBUtil();
    DataTable symbols = GetSelectedSymbols();
    DataTable tradeGrades = GetSelectedTradeGrades();
    DataTable executionGrades = GetSelectedExecutionGrades();        

    chtMyRepeatedChart.DataSource = DB.MyRepeatedCall (
        int.Parse(txtStartBalance.Text),
        int.Parse(ddlTradeTypes.SelectedValue),
        ddlRepeatedTrades.SelectedValue,
        radSide.SelectedValue,
        ddlTradeSetups.SelectedValue,
        symbols,
        ddlChartTimeFrames.SelectedValue,
        int.Parse(ddlHours.SelectedValue),
        int.Parse(ddlYears.SelectedValue),
        int.Parse(ddlMonths.SelectedValue),
        int.Parse(ddlDays.SelectedValue),
        int.Parse(ddlNumSCs.SelectedValue),
        txtDateFrom.Text,
        txtDateTo.Text,
        tradeGrades,
        executionGrades,
        int.Parse(txtMinProfitPips.Text),
        int.Parse(txtMaxProfitPips.Text));

    chtMyRepeatedChart.DataBind();
}

因此,我想替换 DB.MyRepeatedCall chtMyRepeatedChart 并将它们作为参数传递给泛型函数。那可能吗?我的表单上有很多使用相同数量参数的图表。

So, I want to replace DB.MyRepeatedCall, chtMyRepeatedChart and pass them in as parameters to a generic function. Is that possible? I have many charts on my form that take the same number of parameters.

谢谢

更新
按照Frederik的解决方案,我已经这样做:

UPDATE Following Frederik's solution I have done this:

private delegate IEnumerable<DataTable> GetDataSource(
    int TradeType,
    string RepeatedTrades,
    string Side,
    string TradeSetup,
    DataTable symbols,
    string ChartTimeFrame,
    int Hour,
    int Year,
    int Month,
    int Day,
    int NumSCs,
    string DateFrom,
    string DateTo,
    DataTable TradeGrades,
    DataTable ExecutionGrades,
    int MinProfitPips,
    int MaxProfitPips);

private void PopulateControl(Chart chart, GetDataSource getDataSource)
{
    //DBUtil DB = new DBUtil();
    DataTable symbols = GetSelectedItems("symbol", listSymbols);
    DataTable tradeGrades = GetSelectedItems("tradeGrade", listTradeGrades);
    DataTable executionGrades = GetSelectedItems("executionGrade", listExecutionGrades);

    chart.DataSource = getDataSource(
        int.Parse(ddlTradeTypes.SelectedValue),
        ddlRepeatedTrades.SelectedValue,
        radSide.SelectedValue,
        ddlTradeSetups.SelectedValue,
        symbols,
        ddlChartTimeFrames.SelectedValue,
        int.Parse(ddlHours.SelectedValue),
        int.Parse(ddlYears.SelectedValue),
        int.Parse(ddlMonths.SelectedValue),
        int.Parse(ddlDays.SelectedValue),
        int.Parse(ddlNumSCs.SelectedValue),
        txtDateFrom.Text,
        txtDateTo.Text,
        tradeGrades,
        executionGrades,
        int.Parse(txtMinProfitPips.Text),
        int.Parse(txtMaxProfitPips.Text));

    chart.DataBind();        
}       

我使用以下命令调用该函数:

I am calling the function with this command:

PopulateControl (chtEquityCurve, DB.GetAccountBalances());

我在智能感知中得到此错误:
方法'GetAccountBalances'接受0个参数。

I get this error in the intellisense: No overload for method 'GetAccountBalances' takes 0 arguments.

推荐答案

首先,创建一个委托类型(通常我建议人们可以使用可用的 Func 委托之一,但他们最多支持16个输入参数,您有18个)。给它一个适当的名称并定义所有输入参数,以便它们具有正确的类型和描述性名称。使委托返回一个 IEnumerable< T>

First, make a delegate type (typically I recomment people to use one of the available Func delegates, but they support only up to 16 input paramters, you have 18). Give it an appropriate name and define all the input parameters so they have the correct type and descriptive names. Make the delegate return an IEnumerable<T>:

public delegate IEnumerable<WhateverTypeIsReturned> GetDataSource(int firstParam, [...]);

然后,修改 PopulateMyRepeatedControl 像这样:

private void PopulateMyRepeatedControl(Chart chart, GetDataSource getDataSource)
{
    DBUtil DB = new DBUtil();
    DataTable symbols = GetSelectedSymbols();
    DataTable tradeGrades = GetSelectedTradeGrades();
    DataTable executionGrades = GetSelectedExecutionGrades();        

    chart.DataSource = getDataSource (
        int.Parse(txtStartBalance.Text),
        int.Parse(ddlTradeTypes.SelectedValue),
        ddlRepeatedTrades.SelectedValue,
        radSide.SelectedValue,
        ddlTradeSetups.SelectedValue,
        symbols,
        ddlChartTimeFrames.SelectedValue,
        int.Parse(ddlHours.SelectedValue),
        int.Parse(ddlYears.SelectedValue),
        int.Parse(ddlMonths.SelectedValue),
        int.Parse(ddlDays.SelectedValue),
        int.Parse(ddlNumSCs.SelectedValue),
        txtDateFrom.Text,
        txtDateTo.Text,
        tradeGrades,
        executionGrades,
        int.Parse(txtMinProfitPips.Text),
        int.Parse(txtMaxProfitPips.Text));

    chart.DataBind();
}

调用该方法时,只需传递图表和用于收集数据:

When you call the method, simply pass the chart and the method to use for collecting data:

PopulateMyRepeatedControl(oneChart, OneDataCollectionMethod);
PopulateMyRepeatedControl(anotherChart, AnotherDataCollectionMethod);

当然, TheDataCollectionMethod 必须具有正确的签名,否则代码将无法编译。

Of course, TheDataCollectionMethod must have the correct signature, otherwise the code will not compile.

更新

关于您的更新;请注意,您要将方法作为参数传递,而不是调用它:

Update
Regarding your update; note that you want to pass the method as an argument, not invoke it:

PopulateControl (chtEquityCurve, DB.GetAccountBalances);

请注意,方法名称后没有括号。

Note that there are no paranthesis after the method name.

这篇关于如何将方法作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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