C# 3.0 基于输入C将数据存储到对象中#

C# 3.0 基于输入C将数据存储到对象中#,c#-3.0,lambda,automatic-properties,C# 3.0,Lambda,Automatic Properties,我必须根据输入将数据保存到不同的C#对象中 基于“productChoice”的值,我的程序应该将数据保存到相应的类中 例如: 如果productChoice=“auto”,则应将数据设置为AutoDataprefill对象 如果productChoice=“vehicle”,则应将数据设置为VehicleMileage对象 我的订单类别: public class Order { public Products products {get;set;} }

我必须根据输入将数据保存到不同的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; }
    }
  
下面是我创建“order”实例并将数据保存到相应对象的代码:

 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 = , ....}  */

                                   ???
                }
             }
        }   

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

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;

对于这个psuedo代码,我的代码应该是什么样子的:/*如果(productChoice==“auto”){Parameter=,Pnc=,…}elseif(productChoice==“vehicle”){usage=,reportType=,…}*/谢谢。尽管我向成员暗示了“public”,但编译器似乎将成员设置为私有。当我把它放进去的时候。nwOrder之后,intellisense没有向我显示任何内容。这很奇怪,因为我在一个小型web项目中使用了您的类,intellisense在Order类中的工作与预期的一样。也许这和你正在使用的IDE有关。这真的很烦人。运行时会自动将它们设置为private。我不知道如何克服这个问题。这听起来像是IDE中预定义的设置。你用哪一种?
 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 = , ....}  */

                                   ???
                }
             }
        }   
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;