2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Windows.Forms;
9 using DirectOutput.Cab.Toys;
10 using System.Xml.Serialization;
12 using System.Reflection;
14 namespace DirectOutput.Frontend
18 private void UpdateAvailableTypes()
20 DataTable DT =
new DataTable();
21 DT.Columns.Add(
"Toy type", typeof(
string));
22 DT.Columns.Add(
"Source", typeof(
string));
23 Assembly A = System.Reflection.Assembly.GetExecutingAssembly();
25 foreach (Type T
in AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(
IToy).IsAssignableFrom(p) && !p.IsAbstract))
27 DT.Rows.Add(T.Name, (A == T.Assembly ?
"Built in" :
"Scripted"));
30 AvailableToys.ClearSelection();
31 AvailableToys.Columns.Clear();
32 AvailableToys.AutoGenerateColumns =
true;
33 AvailableToys.DataSource = DT;
34 AvailableToys.Refresh();
35 AvailableToys.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
41 private void UpdateTypeXml()
44 if (AvailableToys.SelectedRows.Count > 0)
46 string N = AvailableToys.SelectedRows[0].Cells[0].Value.ToString();
47 General.TypeList Types =
new General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => typeof(
IToy).IsAssignableFrom(p) && !p.IsAbstract));
48 if (Types.Contains(N))
53 object O = Activator.CreateInstance(T);
56 foreach (PropertyInfo PI
in T.GetProperties(BindingFlags.Instance | BindingFlags.Public))
60 if (PI.PropertyType == typeof(
string) && PI.Name ==
"Name")
62 PI.SetValue(O,
"Toy name", null);
64 else if (PI.PropertyType.IsNumber())
68 else if (PI.PropertyType == typeof(
bool))
70 PI.SetValue(O,
false, null);
72 else if (PI.PropertyType == typeof(
string) && PI.Name.ToLower().Contains(
"output"))
74 PI.SetValue(O,
"Name of a output", null);
76 else if (PI.PropertyType == typeof(
string))
78 string V = (string)PI.GetValue(O, null);
79 if (V.IsNullOrWhiteSpace())
81 PI.SetValue(O,
"{0} value".Build(PI.Name), null);
84 else if (PI.PropertyType == typeof(DateTime))
86 PI.SetValue(O, DateTime.MaxValue, null);
93 XmlSerializerNamespaces EmptyNamepsaces =
new XmlSerializerNamespaces(
new[] { System.Xml.XmlQualifiedName.Empty });
94 var Serializer =
new XmlSerializer(T);
95 var Settings =
new System.Xml.XmlWriterSettings();
96 Settings.Indent =
true;
97 Settings.OmitXmlDeclaration =
true;
100 using (var Stream =
new StringWriter())
101 using (var Writer = System.Xml.XmlWriter.Create(Stream, Settings))
103 Serializer.Serialize(Writer, O, EmptyNamepsaces);
104 S = Stream.ToString();
110 S =
"XML Serialization failed.\nException:\n{0}".Build(E.Message);
116 S =
"Type not found";
124 InitializeComponent();
125 UpdateAvailableTypes();
128 private void AvailableToys_SelectionChanged(
object sender, EventArgs e)
133 private void CopyToClipboard_Click(
object sender, EventArgs e)
135 if (!TypeXml.Text.IsNullOrWhiteSpace())
137 Clipboard.SetText(TypeXml.Text);