forked from thedeemon/gep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FieldsToProperties.cs
268 lines (228 loc) · 10.7 KB
/
FieldsToProperties.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
namespace gep
{
class FieldsToPropertiesProxyTypeDescriptor : ICustomTypeDescriptor
{
private object _target;
public FieldsToPropertiesProxyTypeDescriptor(object target)
{
if (target == null) throw new ArgumentNullException("target");
_target = target;
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return _target;
}
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(_target, true);
}
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(_target, true);
}
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(_target, true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(_target, true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(_target, true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return null;// TypeDescriptor.GetDefaultProperty(_target, true);
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return null;// TypeDescriptor.GetEditor(_target, editorBaseType, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(_target, attributes, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(_target, true);
}
private PropertyDescriptorCollection _propCache;
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties(null);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection props = _propCache;
if (props != null)
return props;
// Create the property collection and filter if necessary
props = new PropertyDescriptorCollection(null);
//foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(_target, attributes, true))
//{
// props.Add(prop);
//}
foreach (FieldInfo field in _target.GetType().GetFields())
{
Attribute[] attrs = (Attribute[])field.GetCustomAttributes(typeof(Attribute), true);
int n = attrs.Length;
PropertyDescriptor fieldDesc;
if (field.FieldType.IsPrimitive || field.FieldType.IsEnum || field.FieldType == typeof(Guid)
|| field.FieldType == typeof(System.Drawing.Size))
{
Array.Resize<Attribute>(ref attrs, n + 1);
attrs[n] = new ReadOnlyAttribute(true);
object fld_value = field.GetValue(_target);
string str = fld_value.ToString();
if ((field.FieldType != typeof(Guid)) && (fld_value is IFormattable) && (str.Length > 1)) {
StringBuilder sb = new StringBuilder();
IFormattable frm = (IFormattable)fld_value;
string hex = frm.ToString("X", Thread.CurrentThread.CurrentCulture);
if (field.Name == "Compression")
{
UInt32 ival = UInt32.Parse(str);
char[] fourcc = new char[4];
fourcc[0] = (char)(ival & 0xFF);
fourcc[1] = (char)((ival >> 8) & 0xFF);
fourcc[2] = (char)((ival >> 16) & 0xFF);
fourcc[3] = (char)((ival >> 24) & 0xFF);
sb.Append("'");
sb.Append(fourcc);
sb.Append("' "+str+" (0x" + hex + ")");
}
else
sb.Append(str + " (0x" + hex + ")");
str = sb.ToString();
}
fieldDesc = new CustomFieldPropertyDescriptor(str, attrs, field.Name, _target.GetType());
//fieldDesc = new FieldPropertyDescriptor(field, attrs);
}
else
{
Array.Resize<Attribute>(ref attrs, n + 2);
attrs[n] = new ReadOnlyAttribute(true);
attrs[n + 1] = new TypeConverterAttribute(typeof(ExpandableObjectConverter));
object fld_value = new FieldsToPropertiesProxyTypeDescriptor(field.GetValue(_target));
fieldDesc = new CustomFieldPropertyDescriptor(fld_value, attrs, field.Name, _target.GetType());
}
props.Add(fieldDesc);
}
_propCache = props;
return props;
}
public override string ToString()
{
//return _target.GetType().Name;
Dictionary<string, string> fields = new Dictionary<string, string>();
DumpProps(this as ICustomTypeDescriptor, fields, "");
StringBuilder sb = new StringBuilder();
sb.Append(fval(fields, "{0}", "BmiHeader.Width"));
sb.Append(fval(fields, "x{0} ", "BmiHeader.Height"));
sb.Append(fval(fields, "{0} bit ", "BmiHeader.BitCount"));
sb.Append(fval(fields, "ImgSz={0} ", "BmiHeader.ImageSize"));
sb.Append(fval(fields, "TimePerFrame={0} ", "AvgTimePerFrame"));
sb.Append(fval(fields, "Aspect={0}", "PictAspectRatioX"));
sb.Append(fval(fields, "x{0} ", "PictAspectRatioY"));
sb.Append(fval(fields, "{0} Hz ", "nSamplesPerSec"));
sb.Append(fval(fields, "{0} bit ", "wBitsPerSample"));
sb.Append(fval(fields, "{0} channels ", "nChannels"));
sb.Append(fval(fields, "nBlockAlign={0} ", "nBlockAlign"));
sb.Append(fval(fields, "Bytes/Sec={0} ", "nAvgBytesPerSec"));
sb.Append(fval(fields, "Tag={0} ", "wFormatTag", false));
if (sb.Length < 1) sb.Append(_target.GetType().Name);
return sb.ToString();
}
static string fval(Dictionary<string, string> fields, string fmt, string key, bool strip)
{
string val;
if (fields.TryGetValue(key, out val))
{
int i = val.IndexOf(' ');
if (i >= 0 && strip) val = val.Remove(i);
return string.Format(fmt, val);
}
return "";
}
static string fval(Dictionary<string, string> fields, string fmt, string key)
{
return fval(fields, fmt, key, true);
}
public void DumpProps(ICustomTypeDescriptor td, Dictionary<string, string> fields, string prefix)
{
foreach (PropertyDescriptor pd in td.GetProperties())
{
object val = pd.GetValue(td);
if (val is ICustomTypeDescriptor)
DumpProps(val as ICustomTypeDescriptor, fields, prefix + pd.DisplayName + ".");
else
fields.Add(prefix + pd.DisplayName, val.ToString());
}
}
}
class FieldPropertyDescriptor : PropertyDescriptor
{
private FieldInfo _field;
public FieldPropertyDescriptor(FieldInfo field, Attribute[] attrs)
: base(field.Name, attrs)
{
_field = field;
}
//public FieldInfo Field { get { return _field; } }
public override bool Equals(object obj)
{
FieldPropertyDescriptor other = obj as FieldPropertyDescriptor;
return other != null && other._field.Equals(_field);
}
public override int GetHashCode() { return _field.GetHashCode(); }
public override bool IsReadOnly { get { return true; } }
public override void ResetValue(object component) { }
public override bool CanResetValue(object component) { return false; }
public override bool ShouldSerializeValue(object component) { return false; }
public override Type ComponentType { get { return _field.DeclaringType; } }
public override Type PropertyType { get { return _field.FieldType; } }
public override object GetValue(object component) { return _field.GetValue(component); }
public override void SetValue(object component, object value)
{
_field.SetValue(component, value);
OnValueChanged(component, EventArgs.Empty);
}
}
class CustomFieldPropertyDescriptor : PropertyDescriptor
{
private object _field;
Type compType;
public CustomFieldPropertyDescriptor(object field_value, Attribute[] attrs, string name, Type comp_type)
: base(name, attrs)
{
_field = field_value;
compType = comp_type;
}
//public FieldInfo Field { get { return _field; } }
public override bool Equals(object obj)
{
CustomFieldPropertyDescriptor other = obj as CustomFieldPropertyDescriptor;
return other != null && other._field.Equals(_field);
}
public override int GetHashCode() { return _field.GetHashCode(); }
public override bool IsReadOnly { get { return true; } }
public override void ResetValue(object component) { }
public override bool CanResetValue(object component) { return false; }
public override bool ShouldSerializeValue(object component) { return false; }
public override Type ComponentType { get { return compType; } }
public override Type PropertyType { get { return _field.GetType(); } }
public override object GetValue(object component) { return _field; }
public override void SetValue(object component, object value)
{
//_field.SetValue(component, value);
//OnValueChanged(component, EventArgs.Empty);
}
}
}