列出<t>添加项目</t>

时间:2014-05-07 13:16:15

标签: c#

我有一个这样的模型:

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的语法!

有人可以帮忙吗?

5 个答案:

答案 0 :(得分:8)

那是因为你添加了错误的类型......

您已将funding_instruments声明为List<Funding_Instruments>,但尝试添加新的Credit_Card。为了使它工作,它应该看起来更像这样:

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

答案 1 :(得分:2)

Funding_InstrumentsCredit_Card之间没有任何关系您有一个强类型List<Funding_Instruments>,并且您正尝试在列表中添加Credit_Card类型的新项目。您可以&这样做。也许你的意图是通过设置Funding_Instruments属性来添加新的credit_card

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

答案 2 :(得分:0)

您的列表属于Funding_Instruments类型之一...但是,您尝试添加一个永远不会有效的Credit_Card类型的项目。

答案 3 :(得分: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);

答案 4 :(得分: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"
    }
});