Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用函数向IList集合添加元素_C# - Fatal编程技术网

C# 如何使用函数向IList集合添加元素

C# 如何使用函数向IList集合添加元素,c#,C#,我只是想了解WPF treeview的一个示例 我的目标是用一些存储在列表中的项目填充树视图 例如: public class Person { readonly List<Person> _children = new List<Person>(); public IList<Person> Children { get { return _children; } } public string Na

我只是想了解WPF treeview的一个示例

我的目标是用一些存储在列表中的项目填充树视图

例如:

public class Person
{
    readonly List<Person> _children = new List<Person>();
    public IList<Person> Children
    {
        get { return _children; }
    }

    public string Name { get; set; }
}

//Some added nodes:
    public static Person GetFamilyTree()
    {
        // In a real app this method would access a database.
        return new Person
        {
            Name = "David Weatherbeam",
            Children =
            {
                new Person
                {
                    Name="Alberto Weatherbeam",
                    Children=
                    {
                        new Person
                        {
                            Name="Zena Hairmonger",
                            Children=
                            {
                                new Person
                                {
                                    Name="Sarah Applifunk",
                                }
                            }
                        },
                        new Person
                        {
                            Name="Jenny van Machoqueen",
                            Children=
                            {
                                new Person
                                {
                                    Name="Nick van Machoqueen",
                                },
                                new Person
                                {
                                    Name="Matilda Porcupinicus",
                                },
                                new Person
                                {
                                    Name="Bronco van Machoqueen",
                                }
                            }
                        }
                    }
                }

            }
        };
    }
公共类人物
{
只读列表_children=新列表();
公营儿童
{
获取{return\u children;}
}
公共字符串名称{get;set;}
}
//添加了一些节点:
公共静态Person GetFamilyTree()
{
//在实际应用程序中,此方法将访问数据库。
换人
{
Name=“David Weatherbeam”,
孩子们=
{
新人
{
Name=“Alberto Weatherbeam”,
孩子们=
{
新人
{
Name=“Zena Hairmonger”,
孩子们=
{
新人
{
Name=“Sarah Applifunk”,
}
}
},
新人
{
Name=“Jenny van Machoqueen”,
孩子们=
{
新人
{
Name=“Nick van Machoqueen”,
},
新人
{
Name=“Matilda Porcupinicus”,
},
新人
{
Name=“Bronco van Machoqueen”,
}
}
}
}
}
}
};
}
到目前为止,它仍然有效。
现在,我想用我之前创建的读取文件的列表替换第一个父节点上的静态人员

XDocument ecad = XDocument.Load(openFileDialog.FileName);

GlobalVar.persons = new List<Persons>();    //globally available list
Person person = null;

//Einlesen der XML-Datei in lokale Variable (Model)
foreach(XElement elem in ecad.Descendants("Variable"))
{
    if (elem.Element("Name") != null)
    {
        person = new Person(){ Name = elem.Element("Name").Value };
        persons.Add(person);
    }
}
XDocument ecad=XDocument.Load(openFileDialog.FileName);
GlobalVar.persons=新列表()//全球可用列表
Person=null;
//lokale变量(模型)中的XML数据
foreach(ecad.substands(“变量”)中的XElement元素)
{
if(元素(“名称”)!=null)
{
person=newperson(){Name=elem.Element(“Name”).Value};
人。添加(人);
}
}
现在我想把这些人添加到根人物中

//GlobalVar.List<Persons> persons
public static Person GetFamilyTree()
{
    return new Person{
        Name = "Family",
        Children = persons
    }
}
//GlobalVar.List人员
公共静态Person GetFamilyTree()
{
换人{
Name=“Family”,
儿童=人
}
}
那么,如何用从文件中读取数据的函数替换
Children=new…

我真的很困惑

我认为令人困惑的是,
Children
是一个只读属性,但对于“静态子对象”,它可以工作,而对于IList则不能(因为只读)。有人能解释一下区别吗

您的
GetFamilyTree()
方法使用嵌套对象初始值设定项。更多信息请参考Jon Skeet的答案

如果要从某个源读取数据,然后设置
子属性
,则应向其添加setter。您还可以删除支持字段:

public class Person
{
    public IList<Person> Children { get; set; } = new List<Person>();
    public string Name { get; set; }
}
公共类人物
{
公共IList子项{get;set;}=new List();
公共字符串名称{get;set;}
}

对不起,我上次的评论是错误的。因为persons是一个列表,Children是接口IList,所以您可以只使用Children=persons而不使用任何其他内容?您想替换什么?
子项
只能设置为
IList
。这从哪里来并不重要。哦,是的,我也这么想(并尝试了几种方法,也使用了
IList
)。我认为令人困惑的是,Children是一个只读属性,但对于“静态Children”它可以工作,而对于IList它不能(因为readonly)。有人能解释一下区别吗?好的,非常感谢。对于初学者来说,这有点奇怪;)所以我会像对待塞特和盖特一样去做。是的,当然,对不起