I define an collection initializer with IEnumerable as instructed here:
http://msdn.microsoft.com/en-us/library/bb384062.aspx
Now I'm able to create objects within my collection initializer and they are added wih my Add() method like so:
class ArrangedPanel : RectElement
{
private List<RectElement> arrangedChildren = new List<RectElement>();
public int Padding = 2;
public void Add(RectElement element)
{
arrangedChildren.Add(element);
//do custom stuff here
}
public IEnumerator GetEnumerator()
{
return arrangedChildren.GetEnumerator();
}
}
// Somewhere
debugPanel.Add(new ArrangedPanel()
{
new ButtonToggle(),
new ButtonToggle()
});
However, if I try to set a property, such as my "Padding" field, I get an error on the collection initializers.
debugPanel.Add(new ArrangedPanel()
{
Padding = 5,
new ButtonToggle(),
new ButtonToggle()
});
Is it possible to set both collection initializers and object initializers?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…