0

我必须根据输入将数据保存到不同的 C# 对象中。

基于“productChoice”的值,我的程序应该将数据保存到相应的类。

例如 :

如果 productChoice = "auto" 那么数据应该设置为 AutoDataprefill 对象。

如果 productChoice = "vehicle" 那么数据应该设置为 VehicleMileage 对象。

我的订单类别:

    public class Order
    {
       public Products  products {get;set;}
    }
 

我的产品类:

  public class Products
    {
       public productChoiceType products { get; set; }
    }
  

我的 productChoiceType 类:

   public class productChoiceType
    {
       public string productChoice { get; set; }
       public AutoDataprefill auto_dataprefill { get; set; }
       public VehicleMileage vehicle_mileage { get; set; }
       public ClaimsDiscovery claims_discovery { get; set; }
       public PropertyDataprefill property_dataprefill { get; set; }
       public List  productList { get; set; }
    }
   
 
 public class AutoDataprefill
    {
        public Parameter parameter { get; set; }
        public Pnc pnc { get; set; }
        public ProductReturnFormat format { get; set; }
        public string primary_subject { get; set; } // Attribute // IDREF
    }   
 
 
public class VehicleMileage
    {
       public string usage { get; set; }
       public VmrReportType report_type { get; set; }
       public Vehicles vehicles { get; set; }
       public string subject { get; set; } //Attribute // IDREF

    }
 

这是我创建“订单”实例并将数据保存到相应对象的代码:

  CLUEAuto.Personal.BusinessEntities.Order nwOrder = new CLUEAuto.Personal.BusinessEntities.Order
       {


            products = new CLUEAuto.Personal.BusinessEntities.Products 
            {
               products = new CLUEAuto.Personal.BusinessEntities.productChoiceType 
                {
                    /* if (productChoice == "auto")
                           {Parameter = , Pnc = ,....   }      
                       elseif (productChoice == "vehicle")
                           {usage = , reportType = , ....}  */

                                   ???
                }
             }
        }   
4

1 回答 1

1

如果您以这种方式处理对象的创建,也许会更容易:

string productChoice = "auto";
Order nwOrder = new Order();
Products nwProducts = new Products();
nwOrder.products = nwProducts;
productChoiceType nwPCT = new productChoiceType();
if(productChoice == "auto") {   
  AutoDataprefill adp = new AutoDataprefill();
  //adp.Parameter = ...
  //adp.Pnc = ...
  nwPCT.auto_dataprefill = adp;
}
else if (productChoice == "vehicle")
{
  // etc...
}
nwProducts.products = nwPCT;
于 2010-11-17T23:08:59.423 回答