【问题标题】:How to initialize an object that has nested objects in C#?如何在 C# 中初始化具有嵌套对象的对象?
【发布时间】:2021-04-09 20:03:00
【问题描述】:

我有一个类 BaseRequest,它有另一个类作为它的属性之一。该类有自己的一组属性,其中一些也是类,而其中一些类属性是类。如何初始化 ProductOptions-GroupsToRun 和 SelectionTargets 的 BaseRequest?

BaseRequest.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Redundent.Models
{
    public class BaseRequest
    {
        public ProductOptions ProductOptions { get; set; }
        public string RequestingSystem { get; set; }
        public bool HasDeposit { get; set; }
        public decimal? PurchasePrice { get; set; }
        public decimal? PaymentAmount { get; set; }
    }
}

ProductOptions.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Redundent.Models
{
    public class ProductOptions
    {
        public GroupOption[] GroupsToRun { get; set; }
        public string Requestor { get; set; }
        public SelectionTargets SelectionTargets { get; set; }
        public string Partner { get; set; }

    }
}

GroupOption.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Redundent.Models
{
    public class GroupOption
    {
        public TransportationType TransportationType { get; set; }
        public RegistrationType Type { get; set; }

    }
}

TransportationType 和 RegistrationType 是枚举。交通类型有汽车、飞机、船、巴士和 RegistrationType 有 Ticket、ID、Passport

SelectionTargets.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Redundent.Models
{
    public class SelectionTargets
    {
        public PricingOptionTargets PricingOptionTargets { get; set; }
        public ProductTargets ProductTargets { get; set; }
    }
}

这就是我开始的...

    var baseRequest = new BaseRequest()
            {
                
                ProductOptions = { GroupsToRun = {//not sure what to do here.  Tried TransportationType.Car but got "does not contain a definition for 'Add' and no accessible extension method 'Add'}, Requestor = "Ford", SelectionTarget = {also not sure what to do here}, Partner = 
                "Heuwett B." },

                
            };

【问题讨论】:

    标签: c# class object inner-classes


    【解决方案1】:

    GroupsToRun 字段是一个数组,因此必须使用数组初始化程序来创建数组。查看以下示例:

    var baseRequest = new BaseRequest()
    {
        ProductOptions = new ProductOptions
        {
            GroupsToRun = new[]
            {
                new GroupOption
                {
                    TransportationType = new TransportationType {}
                }
            }, 
            Requestor = "Ford",
            SelectionTargets = new SelectionTargets
            {
                PricingOptionTargets = new PricingOptionTargets(),
                ProductTargets = new ProductTargets()
            }, 
            Partner = "Heuwett B."
        },
    };
    

    【讨论】:

      猜你喜欢
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      相关资源
      最近更新 更多