[英]How to create a List from user defined Classes
Hello everyone I am new to Stackoverflow so ignore the mistakes. 大家好,我是Stackoverflow的新手,所以请忽略这些错误。 I have different user defined classes which have number of attributes.
我有不同的用户定义类,这些类具有许多属性。 I want to create a List with the use of these classes and need to use system define datatype instead of user define classes... Here is the code you can better understand with it.
我想使用这些类创建一个列表,并且需要使用系统定义数据类型而不是用户定义类...这是您可以更好地理解的代码。
Here are the classes 这是课程
public class Slide
{
public string Name { get; set; }
public bool IsChecked { get; set; }
}
//.........
public class SubSection
{
public SubSection()
{
this.Composition = new ObservableCollection<object>();
}
public string Name { get; set; }
public bool IsChecked { get; set; }
public ObservableCollection<object> Composition { get; set; }
}
//................
public class Section
{
public Section()
{
this.SubSections = new List<SubSection>();
}
public string Name { get; set; }
public bool IsChecked { get; set; }
public List<SubSection> SubSections { get; set; }
}
Every Node in the list should have section,subsection and slide 列表中的每个节点都应具有节,小节和幻灯片
I'm assuming that you need a list, where every element in the list contains one of each of the classes you've listed in your question. 我假设您需要一个列表,列表中的每个元素都包含您在问题中列出的每个类之一。 You can use a List of Tuples :
您可以使用元组列表:
var mylist = new List<Tuple<Section, SubSection, Slide>>();
mylist.Add(Tuple.Create(new Section(), new SubSection(), new Slide());
mylist.Add(Tuple.Create(new Section(), new SubSection(), new Slide());
mylist.Add(Tuple.Create(new Section(), new SubSection(), new Slide());
Tuples were introduced in .NET 4.5, so as long as you're on at least 4.5, this will work for you. 元组是在.NET 4.5中引入的,因此只要您至少使用4.5,它就可以为您服务。
First create a model class with all your data you want to include and after that you can create list of that class. 首先使用您要包含的所有数据创建一个模型类,然后可以创建该类的列表。
public class CustomClass
{
public Section{get;set;}
public SubSection{get;set;}
public Slide{get;set;}
}
var customClasses = new List<CustomClass>();
I would agree with Josh Smeaton's answer of Tuples, but for fun I wonder if you could consider Anonymous Types as a System Defined Type...? 我会同意Josh Smeaton对Tuples的回答,但出于娱乐目的,我想知道您是否可以考虑将匿名类型视为系统定义的类型...?
var myList = new[]
{
new { Section = new Section(), SubSection = new SubSection(), Slide = new Slide()},
new { Section = new Section(), SubSection = new SubSection(), Slide = new Slide()}
}.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.