How to pass a Method with parameters to another method

时间:2015-10-29 15:54:58

标签: c# function

I'm trying to build up a Data Matrix which comprises a list of objects. So here's what I am trying to do:

List<IBasePremium> BasePremiumMatrix = new List<IBasePremium>();
List<ICalcRate> calcRates = new List<ICalcRate>
{
    new CalcRate { BasePremiumType = 1, Rate = basePremiumRate.Building, Calc = basePremiumRate.Building },
    new CalcRate { BasePremiumType = 2, Rate = basePremiumProduct.Building,Calc = calculator.BasePremium(basePremiumProduct.Building,basePremiumRate.Building) }
   // new CalcRate { BasePremiumType = 3, Rate = (decimal)postcodeMultiplier.BuildingsCore  ,Calc = calculator.BasePremium(postcodeMultiplier.BuildingsCore,  ) },
};

on my line of code that is commented out, as the second parameter I really want to pass the value of 'Calc' from the previous line of code. I've got a number of lines like this where I need to pass the previous 'Calc' value to build the matrix. The above is clearly the wrong approach and thought that I'd be able to write a method that takes the form something like :
public CalcRate Multiplier(Func<string,decimal>, int basePremiumType, decimal rate) {.....}

But I'm fighting witrh passing the method name and it's parameter values.

2 个答案:

答案 0 :(得分:4)

创建和操作或功能:

Action customAction = ()=> yourFunctionName(param1, param2);

然后将其传递给乘数。

var calcul = Multiplier(customAction , ....);

答案 1 :(得分:0)

for循环怎么样?

List<IBasePremium> BasePremiumMatrix = new List<IBasePremium>(); 
List<ICalcRate> calcRates = new List<ICalcRate>();
for (int i = 1; i < max; i++) {
    CalcRate rate = new CalcRate { BasePremiumType = i , Rate = basePremiumRate.Building };
    if ( i > 1) { 
        rate.Calc = calcRates.get(i - 2)
    } else {
        rate.Calc = calculator.BasePremium(basePremiumProduct.Building,basePremiumRate.Building);
    } 
    calcRates.add(rate);
}