2015-01-03 3 views
-2

재미있는 문제가 있습니다.var와 동적 문 결합하기

저는 var 항목을 반환하는 함수가 있습니다.

public override dynamic GetRates() 

그리고 내가 다른 함수에 반환하고 내 코드에 적용하려고 : 동적 함수에서

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

때 이제

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

I 그것을 적용하려고하면 개체가 존재하지 않는다고합니다. 그러나 디버거를 보면 항목이 일반 목록이나 그렇지 않은 항목으로 행복하게 있습니다.

어떻게 해결할 수 있습니까?

편집 :

요청에 따라 나는 전체 코드를 게시합니다 : dynamic 컴파일 및 실행시에 선택되어 있기 때문에

//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; 

    } 
+0

스타일은 C# 규칙과 실제로 일치하지 않습니다. –

+0

그럼 내가 뭐라 할 수 있겠 어. – misha130

+0

실제로 나는 전체 코드를 게시하고 싶지 않은 이유가 있었기 때문에 신이 그렇게 혼란 스럽다. – misha130

답변

2

컴파일러는 그것에 대해 행복하다. 문제가 무엇이든 형식이 일치하지 않습니다. 런타임에 이것을 평가하므로 컴파일 할 때 이슈가 표시되지 않습니다. 내가 이것을 사용하여 코드를 시도


: (조언! 다른 사람은 문제의이 종류를해야합니다 경우에만 당신이 정말로해야 모든 시간을 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; 
} 
+0

우리는 무엇이'SumList'인지 모른다. 어쩌면 그것은 일부 금액의 목록입니다. –

+0

오타였습니다. 오해로 인해 미안합니다. – misha130

+0

@pwas : 답변을 입력하는 동안 코드가 바뀌 었습니다 ... –