gpt4 book ai didi

c# - 如何声明具有数据成员字典类型的匿名类?

转载 作者:太空宇宙 更新时间:2023-11-03 21:36:21 24 4
gpt4 key购买 nike

我在上课

public class Person
{
public string Name;
public Dictionary<string, object> attr;
public string state = "closed";
}

我想将数据添加到列表中

List<Person> mPer = new List<Person>();

mPer.Add(new Person() { data = "My tasks" },attr = new Dictionary<string, object>() {id= "1",title = "sdfsdf",description = "asdasdasd",rel = "Folder",parentID = DBNull.Value});

最佳答案

您应该将分开,例如:

  attr = new Dictionary<string, object>() { { key, { value } };

在你的情况下:

 attr = new Dictionary<string, object>() { { "myKey", new { id = 2, ... } };

如果要添加多个项目:

attr = new Dictionary<string, object>() {
{ "myKey", new {id = 2, title = "sdfsdf", description = "asdasdasd"} },
{ "myOtherKey", new {id = 3, title = "sdfsdsdf", description = "asdaasdasd" } }
};

注意大括号

关于c# - 如何声明具有数据成员字典类型的匿名类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21603966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com