簡體   English   中英

如何創建可以迭代的類的集合?

[英]How to create a collection of classes that can be iterated over?

我有一個對象的一系列屬性,這些屬性本身就是一個類:

private ClassThing Thing1;
private ClassThing Thing2;
private ClassThing Thing3;

private class ClassThing
{
    public string Name;
    public int Foos;
}

在某些區域,我需要能夠專門訪問每個屬性,例如:

label1.Text = Thing1.Name;

但是,也希望創建一個foreach循環來訪問每個循環,如下所示:

string CombinedString;
foreach(ClassThing Thing in SomeCollection)
{
    CombinedString += Thing.Name;
}

最終結果必須是XML可序列化的。 這些示例非常基礎,但我希望它們能更輕松地說明我的需求。

我嘗試創建這些屬性的字典,但是字典無法XML序列化。 我只想簡單地使一個類的所有這些屬性成員都可以迭代,但是我不確定如何做。

誰能指出我正確的方向?

我希望這可以為您澄清一些事情,因為我不確定我是否理解您的問題。

//many normal classes can be made xml serializable by adding [Serializable] at the top of the class
[Serializable]
private class ClassThing
{
    public string Name { get; set; }
    public int Foos { get; set; }
}

//here we create the objects so you can access them later individually
ClassThing thing1 = new ClassThing { Name = "name1", Foos = 1 };
ClassThing thing2 = new ClassThing { Name = "name2", Foos = 2 };
ClassThing thing3 = new ClassThing { Name = "name3", Foos = 3 };

//this is an example of putting them in a list so you can iterate through them later.
List<ClassThing> listOfThings = new List<ClassThing>();
listOfThings.Add(thing1);
listOfThings.Add(thing2);
listOfThings.Add(thing3);

//iteration example
string combined = string.Empty;
foreach (ClassThing thing in listOfThings)
{
    combined += thing.Name;
}

//you could also have created them directly in the list, if you didnt need to have a reference for them individually, like this:
listOfThings.Add(new ClassThing { Name = "name4", Foos = 4 });

//and more advanced concepts like linq can also help you aggregate your list to make the combined string. the foreach makes the code more readable though. this gives the same result as the foreach above, ignore it if it confuses you :)
string combined = listOfThings.Aggregate(string.Empty, (current, thing) => current + thing.Name);

//Here is an example of how you could serialize the list of ClassThing objects into a file:
using (FileStream fileStream = new FileStream("classthings.xml", FileMode.Create))
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<ClassThing>));
    xmlSerializer.Serialize(fileStream, listOfThings);
}

為了能夠使用此方法序列化對象,它們不能包含構造函數,這就是為什么我們使用new ClassThing{Name="",Foos=0}方式創建它們的原因。

您正在尋找IEnumerable接口的實現。 請參閱此鏈接以快速了解如何實現它。

    class MyClass
{
    private ClassThing Thing1;
    private ClassThing Thing2;
    private ClassThing Thing3;

    internal IEnumerable<ClassThing> GetThings()
    {
            yield return Thing1;
            yield return Thing2;
            yield return Thing3;
    }
    void Test()
    {
        foreach(var thing in this.GetThings())
        {
            //use thing
        }
    }
}
public List<ClassThing> Things = new List<ClassThing>();

然后,您可以運行foreach了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM