简体   繁体   中英

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

I have a series of properties for an object which are themselves a class:

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

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

In some areas I need to be able to access each property specifically, for example:

label1.Text = Thing1.Name;

However, it is also desirable to create a foreach loop to access each one, like this:

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

The end result must be XML serializable. These examples are very basic, but I hope they more easily demonstrate my need.

I tried creating a dictionary of these properties instead, but a dictionary is not XML serializable. I'd like to simply make all of these properties members of a class that itself can be iterated over, but I'm not sure how.

Can anyone point me in the right direction?

I hope this clarifies some things for you, since i am not entirely sure i understand your question.

//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);
}

To be able to serialize the objects using this method, they cannot contain a constructor, which is why we use the new ClassThing{Name="",Foos=0} way of creating them.

You're looking for an implementation of the IEnumerable interface. See this link for a quick description of how to implement it.

    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了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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