forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBindingTypeConverters.cs
More file actions
105 lines (89 loc) · 3.33 KB
/
BindingTypeConverters.cs
File metadata and controls
105 lines (89 loc) · 3.33 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
using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using Splat;
namespace ReactiveUI
{
/// <summary>
/// The default converter, simply converts between types that are equal or
/// can be converted (i.e. Button => UIControl)
/// </summary>
public class EqualityTypeConverter : IBindingTypeConverter
{
public int GetAffinityForObjects(Type fromType, Type toType)
{
if (toType.GetTypeInfo().IsAssignableFrom(fromType.GetTypeInfo())) {
return 100;
}
// NB: WPF is terrible.
if (fromType == typeof (object)) {
return 100;
}
var realType = Nullable.GetUnderlyingType(fromType);
if (realType != null) {
return GetAffinityForObjects(realType, toType);
}
realType = Nullable.GetUnderlyingType(toType);
if (realType != null) {
return GetAffinityForObjects(fromType, realType);
}
return 0;
}
static MethodInfo genericMi = null;
static MemoizingMRUCache<Type, MethodInfo> referenceCastCache = new MemoizingMRUCache<Type, MethodInfo>((t, _) => {
genericMi = genericMi ??
typeof(EqualityTypeConverter).GetRuntimeMethods().First(x => x.Name == "DoReferenceCast");
return genericMi.MakeGenericMethod(new[] {t});
}, RxApp.SmallCacheLimit);
public bool TryConvert(object from, Type toType, object conversionHint, out object result)
{
Contract.Requires(toType != null);
var mi = default(MethodInfo);
lock (referenceCastCache) {
mi = referenceCastCache.Get(toType);
}
try {
result = mi.Invoke(null, new[] {from});
} catch (Exception ex) {
this.Log().WarnException("Couldn't convert object to type: " + toType, ex);
result = null;
return false;
}
return true;
}
public static object DoReferenceCast<T>(object from)
{
var targetType = typeof (T);
var backingNullableType = Nullable.GetUnderlyingType(targetType);
if (backingNullableType == null) {
return (T) from;
}
if (from == null) {
var ut = Nullable.GetUnderlyingType(targetType);
if (ut == null) {
throw new Exception("Can't convert from nullable-type which is null to non-nullable type");
}
return default(T);
}
return (T) Convert.ChangeType(from, backingNullableType, null);
}
}
/// <summary>
/// Calls ToString on types. In WPF, ComponentTypeConverter should win
/// instead of this, since It's Better™.
/// </summary>
public class StringConverter : IBindingTypeConverter
{
public int GetAffinityForObjects(Type fromType, Type toType)
{
return (toType == typeof (string) ? 2 : 0);
}
public bool TryConvert(object from, Type toType, object conversionHint, out object result)
{
// XXX: All Of The Localization
result = from.ToString();
return true;
}
}
}