简体   繁体   English

另一个匿名类内的匿名类的C#数组

[英]C# Array of anonymous class inside another anonymous class

I deserialize a JSon string that holds an object with an array of subobjects. 我反序列化一个JSon字符串,该字符串包含一个带有子对象数组的对象。
The current working solution looks like this: 当前有效的解决方案如下所示:

var definition = new { systems = new subclass[0] };
var ret = JsonConvert.DeserializeAnonymousType(source, definition);

public class subclass
{
    public long id;
}

Is there a way to replace the subclass with another anonymous one? 有没有办法用另一个匿名类替换子类?
I tried using the following, but only get a compiler error: 我尝试使用以下内容,但仅出现编译器错误:

var definition = new { constellations = new{ id=0L }[0] };

I wonder if you could do: 我想知道您是否可以:

var definition = new { systems = MakeEmptyArrayFrom(new { id = 0L}) };
...
static T[] MakeEmptyArrayFrom<T>(T value) => new T[0];

note: if that works, it will probably also work even if you use something like: 注意:如果可行,即使您使用类似的方法也可能可行:

static T[] MakeNullArrayFrom<T>(T value) => null;

as I imagine the library is more interested in the Type than the value. 正如我想象的那样,库对Type兴趣大于对value的兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM