一个列表中的不同属性

Different properties in one list

我有一个 class 让我恶心。 我从我的数据库中获取这些数据,只想操纵事物并将其带回。 让我神经崩溃的class:

public class Part
{
    public string name { get; set; }
    public bool horizontal { get; set; }
    public bool @abstract { get; set; }
    public bool orientationChangable { get; set; }
    public int? shared { get; set; }
    public int count { get; set; }
    public int size { get; set; }
    [JsonProperty(PropertyName = "alu")]
    public bool alu { get; set; }
    [JsonProperty(PropertyName = "paint")]
    public bool? paint { get; set; }
    [JsonProperty(PropertyName = "glue")]
    public bool? glue { get; set; }
    public List<> install { get; set; } // Problem here
    public List<> replace { get; set; } // And here
    public int index { get; set; }

对象可以如下所示:

"install": [
        false,
        false,
        false,
        ""
      ],
      "replace": [
        false,
        false,
        false,
        ""
      ],

or different one like this:

"install": [
        false,
        2,
        ""
      ],
      "replace": [
        false,
        false,
        3
      ],

我标记了有问题的属性。在此属性中可以是布尔值、字符串 and/or 和整数。

我尝试制作 List<dynamic>List<object> 但没有任何效果,我该如何处理包含不同类型的列表?

一个解决方案是返回一个空列表,但我什至不知道如何描述属性来让它工作。任何解决方案?

您应该使用类型参数 TU,并且可以在初始化和声明对象时用类型替换它们。

部分class代码

public class Part<T, U>
{
        public string name { get; set; }
        public bool horizontal { get; set; }
        public bool @abstract { get; set; }
        public bool orientationChangable { get; set; }
        public int? shared { get; set; }
        public int count { get; set; }
        public int size { get; set; }
        [JsonProperty(PropertyName = "alu")]
        public bool alu { get; set; }
        [JsonProperty(PropertyName = "paint")]
        public bool? paint { get; set; }
        [JsonProperty(PropertyName = "glue")]
        public bool? glue { get; set; }
        public List<T> install { get; set; } // Problem solved by using T
        public List<U> replace { get; set; } // And also this one
        public int index { get; set; }
    } 

我已经用一些随机数据初始化了它。

static void Main(string[] args)
        {

            Part<dynamic, dynamic> p = new Part<dynamic, dynamic>()
            {
                name = "test",
                horizontal = true,
                @abstract = true,
                orientationChangable = false,
                shared = 12,
                count = 20,
                size = 23,
                alu = true,
                paint = true,
                glue = true,
                install = new List<dynamic>
                {
                    "Hello" , 34, 23, 34, 23
                },
                replace = new List<dynamic>
                {
                    234, true, "test3"
                },
                index = 234
            };

            var jsonObject = Newtonsoft.Json.JsonConvert.SerializeObject(p);
            Console.ReadKey();
        }

jsonObject 有数据。

处理不同类型列表的唯一可能方法是使用最低的常见类型,在这种情况下可能是 objectdynamic 有效,但与 object 相比没有明显的好处。这是此工作的示例:

var input = "[false,false,123,\"foo\"]";
var result = JsonConvert.DeserializeObject<List<object>>(input);
foreach(var item in result)
{
    Console.WriteLine(item);
}

实例:https://dotnetfiddle.net/LOYEWa

因此您的 class 必须将这两个列表定义为 List<object> 才能使反序列化工作

public class Part
{
    ...
    public List<object> install { get; set; } // No problem here
    public List<object> replace { get; set; } // Nor here
    ...
}