结合var和Dynamic语句

时间:2015-01-03 15:44:47

标签: c# c#-4.0

这是一个有趣的问题。

我有一个返回var项的函数;

  var Items = new { sumList = SumList, ratesList = List, sum = List.Sum() };
        return Items;

来自动态的函数:

    public override dynamic GetRates()

然后我将它返回到我在其他地方的函数并尝试将其应用于我的代码:

     dynamic res = cl.mainC.GetRates();
     List<double> MashkantaSumList = res.sumList;

现在,当我尝试应用它时,它表示该对象不存在。但是,如果我查看调试器中的项目很高兴作为通用列表或不是。

如何解决此问题?

编辑:

根据要求,我会发布完整的代码:

 //virtual
   public virtual dynamic TotalMashkanta(int i, double sum, double ribit, string[] discount)
    {
        return 0;
    }

//override
public override dynamic TotalMashkanta(int i, double sum, double ribit, string[] discount)
    {
        double SumTemp = sum;
        double monthlyRibit = ribit / 12;
        Double permPayPerMont = Financial.Pmt(monthlyRibit, i, sum, 0, DueDate.EndOfPeriod);
        List<double> MashkantaList = new List<double>();
        List<double> MashkantaSumList = new List<double>();
        for (int j = 1; j <= i; j++)
        {
            MashkantaList.Add(Mashkanta(j, sum, ribit, permPayPerMont) * (1 - CalcDiscount((j / 12) + 1, discount)));
            SumTemp = getSum(j, sum, ribit, permPayPerMont * -1); ;
            MashkantaSumList.Add(SumTemp);
        }
        var K_Mashkanta = new { sumList = MashkantaSumList, ratesList = MashkantaList, sum = MashkantaList.Sum() };
        return K_Mashkanta;
    }


  //Function that calls the results


  public void GetSilukinTable(string Path, string ClientID, DAL.Client client, string partner_checked,  string insurance_Amount, string Premiya_Structure_Mashkanta, string Premiya_Life_Mashkanta, string Discount_Life_Mashkanta, string Loan_Period,string Loan_EndDate, string Bank, string Loan_Interest, string Loan_Amount, string Discount_Loan, string AgentNotes, string ManID)
    {

        BL.CalculateLogic.Companies t = BL.CalculateLogic.Companies.כלל;
        if(ManID == "211") t = BL.CalculateLogic.Companies.הפניקס;
        if(ManID == "207") t = BL.CalculateLogic.Companies.הראל;
        if(ManID == "206") t = BL.CalculateLogic.Companies.מנורה;
        if(ManID == "208") t = BL.CalculateLogic.Companies.הכשרה;
        BL.CalculateLogic cl = new BL.CalculateLogic(client, t);
        DateTime LoanEnd = DateTime.Now;
        int months = 0;
        if (DateTime.TryParse(Loan_EndDate, out LoanEnd))
            months = BL.Calculating_Companies.Company.GetMonthsBetween(DateTime.Now, LoanEnd);
        else
            months = Int32.Parse(Loan_Period) * 12;
        string[] Discount = Discount_Loan.Split('-');

        dynamic res = cl.mainC.TotalMashkanta(months, Double.Parse(Loan_Amount), Double.Parse(Loan_Interest.Trim('%')), Discount);
        var MashkantaSumList = res.sumList;
        List<double> MashkantaList = res.ratesList;
        List<double> MashkantaSumListPartner = new List<double>();
        List<double> MashkantaListPartner = new List<double>();
        List<double> MashkantaListSum = res.ratesList;

    }

1 个答案:

答案 0 :(得分:2)

编译器很高兴因为dynamic在运行时被编译和检查。无论问题是什么,类型都不匹配。它在运行时对此进行评估,因此您不会在编译时看到问题。 (建议:只有在你必须的时候才使用dynamic!否则你会一直遇到这类问题!)


我尝试使用此代码并且工作正常:

static dynamic GetRates()
{
    List<double> SumList = new List<double>();
    List<double> List = new List<double>();
    var Items = new { sumList = SumList, ratesList = List, sum = List.Sum() };
    return Items;
}

static void Main(string[] args)
{
    dynamic res = GetRates();
    List<double> MashkantaSumList = res.sumList;
}