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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Xamarin.PropertyEditing.Reflection;
using Xamarin.PropertyEditing.Tests.MockControls;
namespace Xamarin.PropertyEditing.Tests
{
internal class MockNameableEditor :MockObjectEditor, INameableObject
{
public string ObjectName { get; set; } = "Nameable";
public Task<string> GetNameAsync ()
{
return Task.FromResult (ObjectName);
}
public Task SetNameAsync (string name)
{
ObjectName = name;
return Task.FromResult (false);
}
public MockNameableEditor (MockControl control) : base (control)
{ }
}
internal class MockObjectEditor
: IObjectEditor, IObjectEventEditor
{
public MockObjectEditor ()
{
TargetType = Target.GetType ().ToTypeInfo ();
}
public MockObjectEditor (params IPropertyInfo[] properties)
: this()
{
Properties = properties.ToArray ();
}
public MockObjectEditor (IReadOnlyList<IPropertyInfo> properties,
IReadOnlyDictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> assignableTypes)
: this ()
{
Properties = properties;
this.assignableTypes = assignableTypes;
}
public MockObjectEditor (MockControl control)
{
Properties = control.Properties.Values.ToArray();
Events = control.Events.Values.ToArray();
Target = control;
TargetType = Target.GetType ().ToTypeInfo ();
}
public object Target
{
get;
set;
} = new object ();
public ITypeInfo TargetType
{
get;
set;
}
public event EventHandler<EditorPropertyChangedEventArgs> PropertyChanged;
/// <summary>
/// Test helper for non-local values, passes in the property, <see cref="ValueInfo{T}.ValueDescriptor"/>, <see cref="ValueInfo{T}.SourceDescriptor"/>
/// </summary>
public Func<IPropertyInfo, object, object, object> ValueEvaluator
{
get;
set;
}
public IReadOnlyCollection<IPropertyInfo> Properties
{
get;
set;
} = new IPropertyInfo[0];
public IReadOnlyDictionary<IPropertyInfo, KnownProperty> KnownProperties
{
get;
set;
}
public IReadOnlyCollection<IEventInfo> Events
{
get;
set;
} = new IEventInfo[0];
public IObjectEditor Parent
{
get;
set;
}
public IReadOnlyList<IObjectEditor> DirectChildren
{
get;
set;
}
public void ChangeAllProperties ()
{
PropertyChanged?.Invoke (this, new EditorPropertyChangedEventArgs (null));
}
public void RaisePropertyChanged (IPropertyInfo property)
{
PropertyChanged?.Invoke (this, new EditorPropertyChangedEventArgs (property));
}
public Task AttachHandlerAsync (IEventInfo ev, string handlerName)
{
this.events[ev] = handlerName;
return Task.FromResult (true);
}
public Task DetachHandlerAsync (IEventInfo ev, string handlerName)
{
this.events.Remove (ev);
return Task.FromResult (true);
}
public Task<IReadOnlyList<string>> GetHandlersAsync (IEventInfo ev)
{
string handler;
if (this.events.TryGetValue (ev, out handler))
return Task.FromResult<IReadOnlyList<string>> (new[] { handler });
return Task.FromResult<IReadOnlyList<string>> (new string[0]);
}
public Task<AssignableTypesResult> GetAssignableTypesAsync (IPropertyInfo property, bool childTypes)
{
if (this.assignableTypes != null) {
if (!this.assignableTypes.TryGetValue (property, out IReadOnlyList<ITypeInfo> types))
return Task.FromResult (new AssignableTypesResult (Enumerable.Empty<ITypeInfo> ().ToArray ()));
else
return Task.FromResult (new AssignableTypesResult (types));
}
return ReflectionObjectEditor.GetAssignableTypes (property.RealType, childTypes);
}
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task SetValueAsync<T> (IPropertyInfo property, ValueInfo<T> value, PropertyVariation variation = null)
{
if (variation != null)
throw new NotSupportedException(); // TODO
value = new ValueInfo<T> {
CustomExpression = value.CustomExpression,
Source = value.Source,
ValueDescriptor = value.ValueDescriptor,
SourceDescriptor = value.SourceDescriptor,
Value = value.Value
};
if (value.Source != ValueSource.Local && ValueEvaluator != null) {
value.Value = (T)ValueEvaluator (property, value.ValueDescriptor, value.SourceDescriptor);
} else if (value.Source == ValueSource.Unset || (property.ValueSources.HasFlag (ValueSources.Default) && Equals (value.Value, default(T))) && value.ValueDescriptor == null) {
this.values.Remove (property);
PropertyChanged?.Invoke (this, new EditorPropertyChangedEventArgs (property));
return;
}
object softValue = value;
if (typeof(T) != property.Type) {
IPropertyConverter converter = property as IPropertyConverter;
object v;
if (converter != null && converter.TryConvert (value.Value, property.Type, out v)) {
var softType = typeof(ValueInfo<>).MakeGenericType (property.Type);
softValue = Activator.CreateInstance (softType);
softType.GetProperty ("Value").SetValue (softValue, v);
softType.GetProperty ("ValueDescriptor").SetValue (softValue, value.ValueDescriptor);
softType.GetProperty ("Source").SetValue (softValue, value.Source);
softType.GetProperty ("SourceDescriptor").SetValue (softValue, value.SourceDescriptor);
}
if (typeof(T).Name == "IReadOnlyList`1") {
var list = (IReadOnlyList<int>) value.Value;
int iv = 0;
foreach (int flag in list) {
iv |= flag;
}
softValue = new ValueInfo<int> {
Value = iv,
Source = value.Source
};
}
}
// Set to resource won't pass values so we will store it on the info since we just pass it back in GetValue
if (value.Source == ValueSource.Resource && value.SourceDescriptor is Resource) {
Type rt = value.SourceDescriptor.GetType();
if (rt.IsGenericType) {
Type ta = rt.GetGenericArguments ()[0];
if (typeof (T).IsAssignableFrom (ta)) {
PropertyInfo pi = rt.GetProperty ("Value");
value.Value = (T)pi.GetValue (value.SourceDescriptor);
} else {
TypeConverter converter = TypeDescriptor.GetConverter (ta);
if (converter != null && converter.CanConvertTo(typeof(T))) {
PropertyInfo pi = rt.GetProperty ("Value");
value.Value = (T)converter.ConvertTo (pi.GetValue (value.SourceDescriptor), typeof (T));
}
}
}
}
this.values[property] = softValue;
PropertyChanged?.Invoke (this, new EditorPropertyChangedEventArgs (property));
}
public async Task<ValueInfo<T>> GetValueAsync<T> (IPropertyInfo property, PropertyVariation variation = null)
{
if (variation != null)
throw new NotSupportedException (); // TODO
Type tType = typeof(T);
object value;
if (this.values.TryGetValue (property, out value)) {
var info = value as ValueInfo<T>;
if (info != null) {
return new ValueInfo<T> {
CustomExpression = info.CustomExpression,
Source = info.Source,
ValueDescriptor = info.ValueDescriptor,
SourceDescriptor = info.SourceDescriptor,
Value = info.Value
};
} else if (value == null || value is T) {
return new ValueInfo<T> {
Value = (T) value,
Source = ValueSource.Local
};
} else if (tType.Name == "IReadOnlyList`1") {
// start with just supporting ints for now
var predefined = (IReadOnlyDictionary<string, int>)property.GetType().GetProperty(nameof(IHavePredefinedValues<int>.PredefinedValues)).GetValue(property);
var underlyingInfo = value as ValueInfo<int>;
int realValue;
if (value is int i) {
realValue = i;
} else
realValue = ((ValueInfo<int>) value).Value;
var flags = new List<int> ();
foreach (int v in predefined.Values) {
if (v == 0 && realValue != 0)
continue;
if ((realValue & v) == v)
flags.Add (v);
}
return (ValueInfo<T>)Convert.ChangeType(new ValueInfo<IReadOnlyList<int>> {
Value = flags,
Source = underlyingInfo?.Source ?? ValueSource.Local
}, typeof(ValueInfo<T>));
} else {
object sourceDescriptor = null, valueDescriptor = null;
ValueSource source = ValueSource.Local;
Type valueType = value.GetType ();
if (valueType.IsConstructedGenericType && valueType.GetGenericTypeDefinition () == typeof(ValueInfo<>)) {
source = (ValueSource)valueType.GetProperty ("Source").GetValue (value);
sourceDescriptor = valueType.GetProperty (nameof (ValueInfo<T>.SourceDescriptor)).GetValue (value);
valueDescriptor = valueType.GetProperty (nameof (ValueInfo<T>.ValueDescriptor)).GetValue (value);
value = valueType.GetProperty ("Value").GetValue (value);
valueType = valueType.GetGenericArguments ()[0];
}
object newValue;
IPropertyConverter converter = property as IPropertyConverter;
if (converter != null && converter.TryConvert (value, tType, out newValue)) {
return new ValueInfo<T> {
Source = source,
Value = (T)newValue,
ValueDescriptor = valueDescriptor,
SourceDescriptor = sourceDescriptor
};
} else if (typeof(T).IsAssignableFrom (valueType)) {
return new ValueInfo<T> {
Source = source,
Value = (T)value,
ValueDescriptor = valueDescriptor,
SourceDescriptor = sourceDescriptor
};
}
}
}
return new ValueInfo<T> {
Source = (property.ValueSources.HasFlag (ValueSources.Default)) ? ValueSource.Default : ValueSource.Unset,
Value = default(T)
};
}
public Task<ITypeInfo> GetValueTypeAsync (IPropertyInfo property, PropertyVariation variation = null)
{
if (variation != null)
throw new NotImplementedException();
Type type = property.Type;
if (this.values.TryGetValue (property, out object value)) {
Type valueType = value.GetType ();
if (valueType.IsConstructedGenericType && valueType.GetGenericTypeDefinition () == typeof(ValueInfo<>)) {
value = valueType.GetProperty ("Value").GetValue (value);
type = value.GetType ();
} else
type = valueType;
}
var asm = new AssemblyInfo (type.Assembly.FullName, true);
return Task.FromResult<ITypeInfo> (new TypeInfo (asm, type.Namespace, type.Name));
}
#pragma warning restore CS1998
internal readonly IDictionary<IPropertyInfo, object> values = new Dictionary<IPropertyInfo, object> ();
internal readonly IDictionary<IEventInfo, string> events = new Dictionary<IEventInfo, string> ();
internal readonly IReadOnlyDictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> assignableTypes;
}
}
|