2014-05-07 95 views
-4

我有這樣一個模型:列表<T>添加項目

class PayPalModel 
{ 
    public class Payment 
    { 
     public string intent { get; set; } 
     public Payer payer { get; set; } 
     public List<Transaction> transactions { get; set; } 
    } 

    public class Payer 
    { 
     public string payment_method { get; set; } 
     public List<Funding_Instruments> funding_instruments { get; set; } 
    } 

    public class Funding_Instruments 
    { 
     public Credit_Card credit_card { get; set; } 
    } 

    public class Credit_Card 
    { 
     public string number { get; set; } 
     public string type { get; set; } 
     public int expire_month { get; set; } 
     public int expire_year { get; set; } 
     public string cvv2 { get; set; } 
     public string first_name { get; set; } 
     public string last_name { get; set; } 
     public Billing_Address billing_address { get; set; } 
    } 

    public class Billing_Address 
    { 
     public string line1 { get; set; } 
     public string city { get; set; } 
     public string state { get; set; } 
     public string postal_code { get; set; } 
     public string country_code { get; set; } 
    } 

    public class Transaction 
    { 
     public Amount amount { get; set; } 
     public string description { get; set; } 
    } 

    public class Amount 
    { 
     public string total { get; set; } 
     public string currency { get; set; } 
     public Details details { get; set; } 
    } 

    public class Details 
    { 
     public string subtotal { get; set; } 
     public string tax { get; set; } 
     public string shipping { get; set; } 
    } 


} 

,我試圖將值添加到它,但我被陷在如何添加funding_instruments

我曾嘗試以下,但它不會讓我再補充一個新的Credit_Card

這是我的代碼:

PayPalWrapper.PayPalModel.Payment payment = new PayPalWrapper.PayPalModel.Payment(); 

     payment.intent = "sale"; 
     payment.payer = new PayPalWrapper.PayPalModel.Payer(); 
     payment.payer.payment_method = "credit_card"; 

     payment.payer.funding_instruments = new List<PayPalWrapper.PayPalModel.Funding_Instruments>(); 
     payment.payer.funding_instruments.Add(new PayPalModel.Credit_Card() { first_name = "test" }); 

但它給錯誤

Error 1 The best overloaded method match for 'System.Collections.Generic.List<PayPalWrapper.PayPalModel.Funding_Instruments>.Add(PayPalWrapper.PayPalModel.Funding_Instruments)' has some invalid arguments 

我只是不能算出添加Credit_Card的語法!

任何人都可以幫忙嗎?

+0

@Jontatas貝寶的文檔可能不會幫助您解決問題。這是一個向列表添加錯誤類型的情況,就是這樣。 – Joe

+0

請務必閱讀整個錯誤消息。它應該符合'不能從'PayPalWrapper.PayPalModel.Credit_Card'轉換爲'PayPalWrapper.PayPalModel.Funding_Instruments'。這是你做錯了什麼的明確指標。 – sloth

+0

感謝您的反對! - 我們都必須學習...... –

回答

8

那是因爲你要添加的錯誤類型...

您已經聲明funding_instrumentsList<Funding_Instruments>,但嘗試添加一個新的Credit_Card。爲了使工作應該看起來更像是這樣的:

payment.payer.funding_instruments.Add(new PayPalModel.Funding_Instruments 
{ 
    credit_card = new PayPalModel.Credit_Card 
    { 
     first_name = "test" 
    } 
}); 
2

Funding_InstrumentsCredit_Card沒有關係,您有一個強類型List<Funding_Instruments>和你想添加Credit_Card類型的新項目到您的list.You可以」做T that.Maybe你的意圖是通過設置credit_card財產這樣增加新的Funding_Instruments

payment.payer.funding_instruments.Add(new Funding_Instruments 
     { 
      credit_card = new Credit_Card() {first_name = "test"} 
     }); 
0

你的列表是Funding_Instruments類型之一......不過,你嘗試添加Credit_Card這將類型的項目從不工作。

0

替換此行:

payment.payer.funding_instruments.Add(new PayPalModel.Credit_Card() { first_name = "test" }); 

var fi = new Funding_Instrument(); 
fi.credit_card = new PayPalModel.Credit_Card() { first_name = "test" }; 
payment.payer.funding_instruments.Add(fi); 
0

你加入錯誤的類型,因此這個錯誤。 請更改代碼:

來源:

payment.payer.funding_instruments.Add(new PayPalModel.Credit_Card() { first_name = "test" }); 

要:

payment.payer.funding_instruments.Add(new PayPalModel.Funding_Instruments 
{ 
    credit_card = new PayPalModel.Credit_Card 
    { 
     first_name = "test" 
    } 
});