-
Notifications
You must be signed in to change notification settings - Fork 4
/
SerializableControl.cs
executable file
·147 lines (128 loc) · 5.83 KB
/
SerializableControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml.Serialization;
namespace WindowlessControls
{
public class SerializableControl : WindowlessControlProxy
{
protected virtual Stream GetSerializationStream()
{
Type type = this.GetType();
Assembly assembly = type.Assembly;
return assembly.GetManifestResourceStream(string.Format("{0}.{1}.xml", assembly.GetName().Name, type.Name));
}
public void Connect(IWindowlessControl control)
{
Type type = GetType();
WindowlessControlHost.RecurseWindowlessControls(control, (current) =>
{
if (string.IsNullOrEmpty(current.Name))
return true;
PropertyInfo prop = type.GetProperty(current.Name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (prop != null)
{
if (prop.GetValue(this, null) != null)
throw new InvalidOperationException(string.Format("Unable to connect to property {0}. The value is non-null", current.Name));
prop.SetValue(this, current, null);
return true;
}
FieldInfo field = type.GetField(current.Name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (field != null)
{
if (field.GetValue(this) != null)
throw new InvalidOperationException(string.Format("Unable to connect to field {0}. The value is non-null", current.Name));
field.SetValue(this, current);
}
return true;
},
null);
}
protected virtual void OnCreateXmlSerializer(XmlAttributeOverrides overrides, List<Type> additionalTypes, List<Type> additionalControls)
{
if (GetType().Assembly != typeof(IWindowlessControl).Assembly)
{
AddAssemblyControlsToList(GetType().Assembly, additionalControls);
}
}
static void XmlIgnoreAttributeOverrides(XmlAttributeOverrides overrides, Type type, List<string> ignore)
{
List<MemberInfo> members = new List<MemberInfo>();
members.AddRange(type.GetFields());
members.AddRange(type.GetProperties());
XmlAttributes attrs = new XmlAttributes();
attrs.XmlIgnore = true;
foreach (MemberInfo member in members)
{
if (ignore.Contains(member.Name))
continue;
overrides.Add(member.DeclaringType, member.Name, attrs);
}
}
static void XmlIgnoreAttributeOverrides(XmlAttributeOverrides overrides, Type type, params string[] ignore)
{
List<string> ignoreList = new List<string>(ignore);
XmlIgnoreAttributeOverrides(overrides, type, ignoreList);
}
public static void AddAssemblyControlsToList(Assembly assembly, List<Type> controls)
{
Type[] allTypes = assembly.GetTypes();
foreach (Type type in allTypes)
{
if (type.IsGenericType || !type.IsPublic)
continue;
Type[] interfaces = type.GetInterfaces();
foreach (Type iface in interfaces)
{
if (iface == typeof(IWindowlessControl))
controls.Add(type);
}
}
}
IWindowlessControl Load(Stream stream)
{
List<Type> additionalControls = new List<Type>();
AddAssemblyControlsToList(Assembly.GetExecutingAssembly(), additionalControls);
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes xmlArrayItems = new XmlAttributes();
xmlArrayItems.XmlArray = new XmlArrayAttribute("Controls");
overrides.Add(typeof(WindowlessControlHost), "SerializableControls", xmlArrayItems);
overrides.Add(typeof(WindowlessControl), "Controls", xmlArrayItems);
XmlIgnoreAttributeOverrides(overrides, typeof(WindowlessControlHost).BaseType);
List<Type> additionalTypes = new List<Type>();
additionalTypes.Add(typeof(SmartColor));
OnCreateXmlSerializer(overrides, additionalTypes, additionalControls);
foreach (Type type in additionalControls)
{
xmlArrayItems.XmlArrayItems.Add(new XmlArrayItemAttribute(type));
}
XmlSerializer ser = new XmlSerializer(typeof(WindowlessControlHost), overrides, additionalTypes.ToArray(), null, null);
//{
// WindowlessControlHost test = new WindowlessControlHost();
// test.Orientation = WindowlessControls.Orientation.Vertical;
// test.Control = new StackPanel();
// test.Control.Controls.Add(new WindowlessLabel("hello world"));
// MemoryStream mem = new MemoryStream();
// ser.Serialize(mem, test);
// mem.Seek(0, SeekOrigin.Begin);
// string text = new StreamReader(mem).ReadToEnd();
//}
IWindowlessControl ret = ser.Deserialize(stream) as IWindowlessControl;
Connect(ret);
return ret;
}
protected virtual void OnLoad()
{
}
public SerializableControl()
{
using (Stream stream = GetSerializationStream())
{
Control = Load(stream);
}
OnLoad();
}
}
}