需要C#中的动态变量名称用于ebay供稿

时间:2018-01-11 00:47:47

标签: c# asp.net-mvc ebay-api

我需要这些可变名称是动态的。 为了举例说明我正在看的内容> https://ebaydts.com/eBayKBDetails?KBid=1742

现在在上面的链接中,它有一些变量代码。

    //Specify Variations
    VariationTypeCollection VarCol = new VariationTypeCollection();

    //Variation 1 - Black S
    VariationType var1 = new VariationType();
    var1.SKU = "VAR1";
    var1.Quantity = 10;
    var1.StartPrice = new AmountType();
    var1.StartPrice.currencyID = CurrencyCodeType.AUD;
    var1.StartPrice.Value = 35;
    var1.VariationSpecifics = new NameValueListTypeCollection();

    NameValueListType Var1Spec1   = new NameValueListType();
    StringCollection Var1Spec1Valuecoll  = new StringCollection();

    Var1Spec1.Name = "Colour";
    Var1Spec1Valuecoll.Add("Black");
    Var1Spec1.Value = Var1Spec1Valuecoll;

    var1.VariationSpecifics.Add(Var1Spec1);

    NameValueListType Var1Spec2 = new NameValueListType();
    StringCollection Var1Spec2Valuecoll = new StringCollection();

    Var1Spec2.Name = "Size";
    Var1Spec2Valuecoll.Add("S");
    Var1Spec2.Value = Var1Spec2Valuecoll;

    var1.VariationSpecifics.Add(Var1Spec2);

    VarCol.Add(var1);

    //Variation 2 - Black L
    VariationType var2 = new VariationType();
    var2.SKU = "VAR2";
    var2.Quantity = 10;
    var2.StartPrice = new AmountType();
    var2.StartPrice.currencyID = CurrencyCodeType.AUD;
    var2.StartPrice.Value = 45;

    var2.VariationSpecifics = new NameValueListTypeCollection();

    NameValueListType Var2Spec1 = new NameValueListType();
    StringCollection Var2Spec1Valuecoll = new StringCollection();

    Var2Spec1.Name = "Colour";
    Var2Spec1Valuecoll.Add("Black");
    Var2Spec1.Value = Var2Spec1Valuecoll;

    var2.VariationSpecifics.Add(Var2Spec1);

    NameValueListType Var2Spec2 = new NameValueListType();
    StringCollection Var2Spec2Valuecoll = new StringCollection();

    Var2Spec2.Name = "Size";
    Var2Spec2Valuecoll.Add("L");
    Var2Spec2.Value = Var2Spec2Valuecoll;

    var2.VariationSpecifics.Add(Var2Spec2);

    VarCol.Add(var2); 

所以我正在使用的代码我想动态创建可变名称,因为你可以想象我无法知道每个产品有多少变化。

当然,我可以计算变化量,并设置一个if else结构

if (count == 1) {
     //some code
}
else if (count == 2) {
     //some code
} 

但这似乎不是一个好的解决方案。

此时需要在每个循环内输出变量

以下是我正在使用的代码:

string UPC = "";
                string Brand = "";
                string MPN = "";

                if (!String.IsNullOrEmpty(product.Gtin))
                    UPC = product.Gtin;

                if (!String.IsNullOrEmpty(ebayProduct.EbayProductBrandName))
                    Brand = ebayProduct.EbayProductBrandName;

                if (!String.IsNullOrEmpty(product.ManufacturerPartNumber))
                    MPN = product.ManufacturerPartNumber;

                //create the call object
                AddFixedPriceItemCall AddFPItemCall = new AddFixedPriceItemCall(context);

                AddFPItemCall.AutoSetItemUUID = true;

                //create an item object and set the properties
                ItemType item = new ItemType();

                //set the item condition depending on the value from GetCategoryFeatures
                item.ConditionID = 1000; //new

                //Basic properties of a listing
                item.Country = CountryCodeType.AU;
                item.Currency = CurrencyCodeType.AUD;

                //Track item by SKU
                item.InventoryTrackingMethod = InventoryTrackingMethodCodeType.SKU;
                item.SKU = ebayProduct.EbayProductSKU;

                if (!String.IsNullOrEmpty(ebayProduct.EbayProductDescription))
                    item.Description = ebayProduct.EbayProductDescription;
                else
                    item.Description = "This product is brand new.";

                if (!String.IsNullOrEmpty(ebayProduct.EbayProductTitle))
                    item.Title = ebayProduct.EbayProductTitle;

                item.SubTitle = ebayProduct.EbayProductItemSubtitle;
                item.ListingDuration = "GTC";

                item.ItemSpecifics = new NameValueListTypeCollection();

                NameValueListTypeCollection ItemSpecs = new NameValueListTypeCollection();

                var productSpecAttributes = product.ProductSpecificationAttributes.ToList();

                foreach (var val in productSpecAttributes)
                {
                    StringCollection valueCol1 = new StringCollection();
                    NameValueListType nv1 = new NameValueListType();

                    nv1.Name = val.SpecificationAttributeOption.SpecificationAttribute.Name.Replace(":", "");
                    valueCol1.Add(val.CustomValue);
                    nv1.Value = valueCol1;

                    ItemSpecs.Add(nv1);
                }

                item.ItemSpecifics = ItemSpecs;

                item.PaymentMethods = new BuyerPaymentMethodCodeTypeCollection();
                item.PaymentMethods.Add(BuyerPaymentMethodCodeType.PayPal);
                item.PayPalEmailAddress = "test@test.com";
                item.PostalCode = "5000";

                //Specify Shipping Services
                item.DispatchTimeMax = 3;
                item.ShippingDetails = new ShippingDetailsType();
                item.ShippingDetails.ShippingServiceOptions = new ShippingServiceOptionsTypeCollection();

                ShippingServiceOptionsType shipservice1 = new ShippingServiceOptionsType();
                shipservice1.ShippingService = "AU_Regular";
                shipservice1.ShippingServicePriority = 1;
                shipservice1.ShippingServiceCost = new AmountType();
                shipservice1.ShippingServiceCost.currencyID = CurrencyCodeType.AUD;
                shipservice1.ShippingServiceCost.Value = 1.0;

                shipservice1.ShippingServiceAdditionalCost = new AmountType();
                shipservice1.ShippingServiceAdditionalCost.currencyID = CurrencyCodeType.AUD;
                shipservice1.ShippingServiceAdditionalCost.Value = 1.0;

                item.ShippingDetails.ShippingServiceOptions.Add(shipservice1);

                ShippingServiceOptionsType shipservice2 = new ShippingServiceOptionsType();
                shipservice2.ShippingService = "AU_Express";
                shipservice2.ShippingServicePriority = 2;
                shipservice2.ShippingServiceCost = new AmountType();
                shipservice2.ShippingServiceCost.currencyID = CurrencyCodeType.AUD;
                shipservice2.ShippingServiceCost.Value = 4.0;

                shipservice2.ShippingServiceAdditionalCost = new AmountType();
                shipservice2.ShippingServiceAdditionalCost.currencyID = CurrencyCodeType.AUD;
                shipservice2.ShippingServiceAdditionalCost.Value = 1.0;

                item.ShippingDetails.ShippingServiceOptions.Add(shipservice2);

                //Specify Return Policy
                item.ReturnPolicy = new ReturnPolicyType();
                item.ReturnPolicy.ReturnsAcceptedOption = "ReturnsAccepted";

                item.StartPrice = new AmountType();
                item.StartPrice.currencyID = CurrencyCodeType.AUD;
                item.StartPrice.Value = Double.Parse(ebayProduct.EbayProductPrice.ToString());

                item.PrimaryCategory = new CategoryType();
                item.PrimaryCategory.CategoryID = ebayProduct.EbayCategoryID;

                item.ProductListingDetails = new ProductListingDetailsType();
                //Specifying UPC as the product identifier. Other applicable product identifiers
                //include ISBN, EAN, Brand-MPN.
                item.ProductListingDetails.UPC = UPC;

                //If multiple product identifiers are specified, eBay uses the first one that
                //matches a product in eBay's catalog system.
                item.ProductListingDetails.BrandMPN = new BrandMPNType();
                item.ProductListingDetails.BrandMPN.Brand = ebayProduct.EbayProductBrandName;
                item.ProductListingDetails.BrandMPN.MPN = product.ManufacturerPartNumber;

                //If multiple prod matches found, list the item with the 1st product's information
                item.ProductListingDetails.UseFirstProduct = true;

                //Add pictures
                item.PictureDetails = new PictureDetailsType();
                item.PictureDetails.PictureURL = new StringCollection();

                //Specify GalleryType
                item.PictureDetails.GalleryType = GalleryTypeCodeType.None;
                item.PictureDetails.GalleryTypeSpecified = true;
                /*
                 *  Handle Variations
                 */
                var childProducts = _productService.GetAssociatedProducts(product.Id);

                if (childProducts != null)
                {
                    //Specify VariationsSpecificsSet
                    item.Variations = new VariationsType();

                    item.Variations.VariationSpecificsSet = new NameValueListTypeCollection();

                    var colourSpec = productSpecAttributes.FirstOrDefault(
                                            x => x.SpecificationAttributeOption.SpecificationAttribute.Name == "Colour");
                    var sizeSpec = productSpecAttributes.FirstOrDefault(
                                    x => x.SpecificationAttributeOption.SpecificationAttribute.Name == "Size");


                    if (colourSpec != null && sizeSpec == null)
                    {
                        List<string> colourSpecsList = new List<string>();

                        foreach (var cp in childProducts)
                        {
                            var colourSpecs = cp.ProductSpecificationAttributes.FirstOrDefault(
                                                x => x.SpecificationAttributeOption.SpecificationAttribute.Name == "Colour");

                            colourSpecsList.Add(colourSpecs.SpecificationAttributeOption.Name);
                        }
                        //sizes
                        NameValueListType NVListVS2 = new NameValueListType();
                        NVListVS2.Name = "Colour";
                        StringCollection VSvaluecollection2 = new StringCollection();
                        String[] Colour = colourSpecsList.ToArray();

                        VSvaluecollection2.AddRange(Colour);

                        NVListVS2.Value = VSvaluecollection2;
                        item.Variations.VariationSpecificsSet.Add(NVListVS2);
                    }

                    //Add Variation Specific Pictures
                    item.Variations.Pictures = new PicturesTypeCollection();

                    foreach (var cp in childProducts)
                    {
                        //Specify Variations
                        VariationTypeCollection VarCol = new VariationTypeCollection();


                        PicturesType pic = new PicturesType();

                        var specs = cp.ProductSpecificationAttributes.FirstOrDefault(x => x.SpecificationAttributeOption.SpecificationAttribute.Name == "Colour");
                        var childEbayProduct = _ebayProductService.GetEbayProductById(cp.Id);

                        if (!String.IsNullOrEmpty(specs.SpecificationAttributeOption.Name))
                        {
                            VariationType var1 = new VariationType();
                            var1.SKU = cp.RexProductId.ToString();
                            Debug.WriteLine(cp.RexProductId.ToString());
                            var1.Quantity = cp.StockQuantity;

                            var1.StartPrice = new AmountType();
                            var1.StartPrice.currencyID = CurrencyCodeType.AUD;
                            var1.StartPrice.Value = Double.Parse(cp.Price.ToString());
                            var1.VariationSpecifics = new NameValueListTypeCollection();

                            NameValueListType Var1Spec1 = new NameValueListType();
                            StringCollection Var1Spec1Valuecoll = new StringCollection();

                            Var1Spec1.Name = "Colour";
                            Var1Spec1Valuecoll.Add(specs.SpecificationAttributeOption.Name);
                            Var1Spec1.Value = Var1Spec1Valuecoll;

                            var1.VariationSpecifics.Add(Var1Spec1);

                            //pics
                            pic.VariationSpecificName = "Colour";
                            pic.VariationSpecificPictureSet = new VariationSpecificPictureSetTypeCollection();
                            VariationSpecificPictureSetType VarPicSet1 = new VariationSpecificPictureSetType();
                            VarPicSet1.VariationSpecificValue = specs.SpecificationAttributeOption.Name;
                            StringCollection PicURLVarPicSet1 = new StringCollection();
                            PicURLVarPicSet1.Add(childEbayProduct.EbayProductMainImgUrl);
                            VarPicSet1.PictureURL = PicURLVarPicSet1;

                            pic.VariationSpecificPictureSet.Add(VarPicSet1);
                            item.Variations.Pictures.Add(pic);

                            VarCol.Add(var1);

                            item.Variations.Variation = VarCol;
                        }
                    }
                }

任何人都知道我在这里可以做些什么。 干杯

1 个答案:

答案 0 :(得分:0)

最接近&#34;动态变量名称&#34;是一本字典。

Dictionary<string, string> variables = new Dictionary<string, string>();
variables.Add("var1", "test1");
variables.Add("var2", "test2");
MessageBox.Show(variables["var1"]); // will display test1

如果每个&#34;变量&#34;都有一组所需数据,请创建一个类或结构(也许称之为&#34; TheData&#34;)

Dictionary<string, TheData> variables = new Dictionary<string, TheData>();
variables.Add("var1", new TheData() { price = 25.28, upc = "01121...", manufacturer = "ACME" });