DirectOuput Framework R2
DirectOutput framework R2 for virtual pinball cabinets
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
OutputList.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 using System.Xml;
4 using System.Xml.Serialization;
5 using DirectOutput.General.Generic;
6 
7 namespace DirectOutput.Cab.Out
8 {
9 
13  public class OutputList : NamedItemList<IOutput>, IXmlSerializable
14  {
15 
16  #region IXmlSerializable implementation
17 
18 
19 
20 
21 
22  public void WriteXml(XmlWriter writer)
23  {
24 
25  XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces();
26  Namespaces.Add(string.Empty, string.Empty);
27  foreach (IOutput E in this)
28  {
29  XmlSerializer serializer = new XmlSerializer(E.GetType());
30  serializer.Serialize(writer, E, Namespaces);
31  }
32  }
33 
34 
40  public void ReadXml(XmlReader reader)
41  {
42  if (reader.IsEmptyElement)
43  {
44  reader.ReadStartElement();
45  return;
46  }
47  General.TypeList Types = new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(IOutput).IsAssignableFrom(p) && !p.IsAbstract));
48 
49  reader.Read();
50 
51  while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
52  {
53  Type T = Types[reader.LocalName];
54  if (T != null)
55  {
56  XmlSerializer serializer = new XmlSerializer(T);
57  IOutput O = (IOutput)serializer.Deserialize(reader);
58  if (!Contains(O.Name))
59  {
60  Add(O);
61  }
62  }
63  else
64  {
65  reader.Skip();
66  }
67  }
68 
69  reader.ReadEndElement();
70  }
71 
72 
79  public System.Xml.Schema.XmlSchema GetSchema() { return (null); }
80  #endregion
81 
82 
83  #region Events
84 
85  #region Event handling
86 
87  void OutputList_BeforeClear(object sender, EventArgs e)
88  {
89  foreach (IOutput O in this)
90  {
91  O.ValueChanged -= new Output.ValueChangedEventHandler(Item_ValueChanged);
92  }
93  }
94 
95  void OutputList_AfterRemove(object sender, RemoveEventArgs<IOutput> e)
96  {
97  e.Item.ValueChanged -= new Output.ValueChangedEventHandler(Item_ValueChanged);
98  }
99 
100  void OutputList_AfterInsert(object sender, InsertEventArgs<IOutput> e)
101  {
102  e.Item.ValueChanged += new Output.ValueChangedEventHandler(Item_ValueChanged);
103  }
104 
105  void OutputList_AfterSet(object sender, SetEventArgs<IOutput> e)
106  {
107  e.OldItem.ValueChanged -= new Output.ValueChangedEventHandler(Item_ValueChanged);
108  e.NewItem.ValueChanged += new Output.ValueChangedEventHandler(Item_ValueChanged);
109  }
110 
111  void Item_ValueChanged(object sender, OutputEventArgs e)
112  {
113  OnOutputValueChanged(e.Output);
114  }
115 
116  #endregion
117 
118  #region ValueChanged Event
119 
120 
125  protected void OnOutputValueChanged(IOutput Output)
126  {
127  if (OutputValueChanged != null)
128  {
129  OutputValueChanged(this, new OutputEventArgs(Output));
130  }
131  }
132 
136  public event OutputValueChangedEventHandler OutputValueChanged;
137 
143  public delegate void OutputValueChangedEventHandler(object sender, OutputEventArgs e);
144 
145 
146 
147  #endregion
148  #endregion
149 
153  public OutputList()
154  {
155  this.AfterInsert += new EventHandler<InsertEventArgs<IOutput>>(OutputList_AfterInsert);
156  this.AfterRemove += new EventHandler<RemoveEventArgs<IOutput>>(OutputList_AfterRemove);
157  this.AfterSet += new EventHandler<SetEventArgs<IOutput>>(OutputList_AfterSet);
158  this.BeforeClear += new EventHandler<EventArgs>(OutputList_BeforeClear);
159  }
160 
161 
162 
166  ~OutputList()
167  {
168  foreach (IOutput O in this)
169  {
170  O.ValueChanged -= new Output.ValueChangedEventHandler(Item_ValueChanged);
171  }
172  }
173 
174 
175 
176 
177 
178  }
179 }