-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathXamlPropertyInfo.cs
More file actions
376 lines (319 loc) · 10.9 KB
/
XamlPropertyInfo.cs
File metadata and controls
376 lines (319 loc) · 10.9 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;
namespace ICSharpCode.WpfDesign.XamlDom
{
/// <summary>
/// Represents a property assignable in XAML.
/// This can be a normal .NET property or an attached property.
/// </summary>
internal abstract class XamlPropertyInfo
{
public abstract object GetValue(object instance);
public abstract void SetValue(object instance, object value);
public abstract void ResetValue(object instance);
public abstract TypeConverter TypeConverter { get; }
public abstract Type TargetType { get; }
public abstract Type ReturnType { get; }
public abstract string Name { get; }
public abstract string FullyQualifiedName { get; }
public abstract bool IsAttached { get; }
public abstract bool IsCollection { get; }
public virtual bool IsEvent { get { return false; } }
public virtual bool IsAdvanced { get { return false; } }
public virtual DependencyProperty DependencyProperty { get { return null; } }
public abstract string Category { get; }
}
#region XamlDependencyPropertyInfo
internal class XamlDependencyPropertyInfo : XamlPropertyInfo
{
readonly DependencyProperty property;
readonly bool isAttached;
readonly bool isCollection;
readonly string dependencyPropertyGetterName;
Func<object, object> attachedGetter;
public override DependencyProperty DependencyProperty {
get { return property; }
}
public XamlDependencyPropertyInfo(DependencyProperty property, bool isAttached, string dependencyPropertyGetterName, Func<object, object> attachedGetter = null)
{
Debug.Assert(property != null);
this.property = property;
this.isAttached = isAttached;
this.isCollection = CollectionSupport.IsCollectionType(property.PropertyType);
this.attachedGetter = attachedGetter;
this.dependencyPropertyGetterName = dependencyPropertyGetterName;
}
public override TypeConverter TypeConverter {
get {
return TypeDescriptor.GetConverter(this.ReturnType);
}
}
public override string FullyQualifiedName {
get {
return this.TargetType.FullName + "." + this.Name;
}
}
public override Type TargetType {
get { return property.OwnerType; }
}
public override Type ReturnType {
get { return property.PropertyType; }
}
public override string Name {
get { return dependencyPropertyGetterName; }
}
public override string Category {
get { return "Misc"; }
}
public override bool IsAttached {
get { return isAttached; }
}
public override bool IsCollection {
get {
return isCollection;
}
}
public override object GetValue(object instance)
{
try {
if (attachedGetter != null) {
return attachedGetter(instance);
}
} catch (Exception ex) {
Debug.WriteLine($"Attached property getter failed: {ex.Message}");
}
var dependencyObject = instance as DependencyObject;
if (dependencyObject != null) {
return dependencyObject.GetValue(property);
}
return null;
}
public override void SetValue(object instance, object value)
{
((DependencyObject)instance).SetValue(property, value);
}
public override void ResetValue(object instance)
{
((DependencyObject)instance).ClearValue(property);
}
}
#endregion
#region XamlNormalPropertyInfo
internal sealed class XamlNormalPropertyInfo : XamlPropertyInfo
{
PropertyDescriptor _propertyDescriptor;
DependencyProperty dependencyProperty;
public XamlNormalPropertyInfo(PropertyDescriptor propertyDescriptor)
{
this._propertyDescriptor = propertyDescriptor;
var dpd = DependencyPropertyDescriptor.FromProperty(propertyDescriptor);
if (dpd != null) {
dependencyProperty = dpd.DependencyProperty;
}
}
public override DependencyProperty DependencyProperty {
get {
return dependencyProperty;
}
}
public override object GetValue(object instance)
{
return _propertyDescriptor.GetValue(instance);
}
public override void SetValue(object instance, object value)
{
_propertyDescriptor.SetValue(instance, value);
}
public override void ResetValue(object instance)
{
try {
_propertyDescriptor.ResetValue(instance);
} catch (Exception ex) {
// For Example "UndoRedoSimpleBinding" will raise an exception here
Debug.WriteLine($"ResetValue failed for {_propertyDescriptor.Name}: {ex.Message}");
}
}
public override Type ReturnType {
get { return _propertyDescriptor.PropertyType; }
}
public override Type TargetType {
get { return _propertyDescriptor.ComponentType; }
}
public override string Category {
get { return _propertyDescriptor.Category; }
}
public override TypeConverter TypeConverter {
get {
return GetCustomTypeConverter(_propertyDescriptor.PropertyType) ?? _propertyDescriptor.Converter;
}
}
public override string FullyQualifiedName {
get {
return _propertyDescriptor.ComponentType.FullName + "." + _propertyDescriptor.Name;
}
}
public override string Name {
get { return _propertyDescriptor.Name; }
}
public override bool IsAttached {
get { return false; }
}
public override bool IsCollection {
get {
return CollectionSupport.IsCollectionType(_propertyDescriptor.PropertyType);
}
}
public override bool IsAdvanced {
get {
var a = _propertyDescriptor.Attributes[typeof(EditorBrowsableAttribute)] as EditorBrowsableAttribute;
if (a != null) {
return a.State == EditorBrowsableState.Advanced;
}
return false;
}
}
public static readonly TypeConverter StringTypeConverter = TypeDescriptor.GetConverter(typeof(string));
public static TypeConverter GetCustomTypeConverter(Type propertyType)
{
if (propertyType == typeof(object))
return StringTypeConverter;
else if (propertyType == typeof(Type))
return TypeTypeConverter.Instance;
else if (propertyType == typeof(DependencyProperty))
return DependencyPropertyConverter.Instance;
else
return null;
}
sealed class TypeTypeConverter : TypeConverter
{
public readonly static TypeTypeConverter Instance = new TypeTypeConverter();
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null)
return null;
if (value is string) {
IXamlTypeResolver xamlTypeResolver = (IXamlTypeResolver)context.GetService(typeof(IXamlTypeResolver));
if (xamlTypeResolver == null)
throw new XamlLoadException("IXamlTypeResolver not found in type descriptor context.");
return xamlTypeResolver.Resolve((string)value);
} else {
return base.ConvertFrom(context, culture, value);
}
}
}
sealed class DependencyPropertyConverter : TypeConverter
{
public readonly static DependencyPropertyConverter Instance = new DependencyPropertyConverter();
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null)
return null;
if (value is string) {
XamlTypeResolverProvider xamlTypeResolver = (XamlTypeResolverProvider)context.GetService(typeof(XamlTypeResolverProvider));
if (xamlTypeResolver == null)
throw new XamlLoadException("XamlTypeResolverProvider not found in type descriptor context.");
XamlPropertyInfo prop = xamlTypeResolver.ResolveProperty((string)value);
if (prop == null)
throw new XamlLoadException("Could not find property " + value + ".");
XamlDependencyPropertyInfo depProp = prop as XamlDependencyPropertyInfo;
if (depProp != null)
return depProp.DependencyProperty;
FieldInfo field = prop.TargetType.GetField(prop.Name + "Property", BindingFlags.Public | BindingFlags.Static);
if (field != null && field.FieldType == typeof(DependencyProperty)) {
return (DependencyProperty)field.GetValue(null);
}
throw new XamlLoadException("Property " + value + " is not a dependency property.");
} else {
return base.ConvertFrom(context, culture, value);
}
}
}
}
#endregion
#region XamlEventPropertyInfo
sealed class XamlEventPropertyInfo : XamlPropertyInfo
{
readonly EventDescriptor _eventDescriptor;
public XamlEventPropertyInfo(EventDescriptor eventDescriptor)
{
this._eventDescriptor = eventDescriptor;
}
public override object GetValue(object instance)
{
throw new NotSupportedException();
}
public override void SetValue(object instance, object value)
{
}
public override void ResetValue(object instance)
{
}
public override Type ReturnType {
get { return _eventDescriptor.EventType; }
}
public override Type TargetType {
get { return _eventDescriptor.ComponentType; }
}
public override string Category {
get { return _eventDescriptor.Category; }
}
public override TypeConverter TypeConverter {
get { return XamlNormalPropertyInfo.StringTypeConverter; }
}
public override string FullyQualifiedName {
get {
return _eventDescriptor.ComponentType.FullName + "." + _eventDescriptor.Name;
}
}
public override string Name {
get { return _eventDescriptor.Name; }
}
public override bool IsEvent {
get { return true; }
}
public override bool IsAttached {
get { return false; }
}
public override bool IsCollection {
get { return false; }
}
}
#endregion
}