根据输入 C# 将数据存储到对象中

发布于 2024-10-02 16:00:00 字数 2167 浏览 0 评论 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; }
    }
  

我的产品选择类型类:

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

                                   ???
                }
             }
        }   

I have to save data into different C# objects based on the input.

Based on the value of "productChoice" my program should save data to the corresponding class.

For example :

if productChoice = "auto" then the data shoud be set to AutoDataprefill object.

if productChoice = "vehicle" then the data shoud be set to VehicleMileage object.

My order class :

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

My Products class :

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

My productChoiceType class :

   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

    }
 

Here is my code to create an instance of "order" and save data to corresponding objects:

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

                                   ???
                }
             }
        }   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等往事风中吹 2024-10-09 16:00:00

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

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;

Maybe it would be easier if you handle the creation of your objects this way:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文