-
Notifications
You must be signed in to change notification settings - Fork 14
/
DynamicMultiConverter.cs
178 lines (156 loc) · 6.54 KB
/
DynamicMultiConverter.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Windows;
using System.Windows.Data;
using Microsoft.CSharp.RuntimeBinder;
using Expression = System.Linq.Expressions.Expression;
namespace QuickConverter
{
public class DynamicMultiConverter : IMultiValueConverter
{
private static Type _namedObjectType = typeof(DependencyObject).Assembly.GetType("MS.Internal.NamedObject", false);
private static ConcurrentDictionary<Type, Func<object, object>> castFunctions = new ConcurrentDictionary<Type, Func<object, object>>();
public string ConvertExpression { get; private set; }
public string[] ConvertBackExpression { get; private set; }
public Exception LastException { get; private set; }
public int ExceptionCount { get; private set; }
public string ConvertExpressionDebugView { get; private set; }
public string[] ConvertBackExpressionDebugView { get; private set; }
public Type[] PTypes { get; private set; }
public Type ValueType { get; private set; }
public object[] Values { get { return _values; } }
private int[] _pIndices;
private Func<object[], object[], object> _converter;
private Func<object, object[], object>[] _convertBack;
private object[] _values;
private DataContainer[] _toDataContainers;
private DataContainer[] _fromDataContainers;
private IValueConverter _chainedConverter;
public DynamicMultiConverter(Func<object[], object[], object> converter, Func<object, object[], object>[] convertBack, object[] values, string convertExp, string convertExpDebug, string[] convertBackExp, string[] convertBackExpDebug, Type[] pTypes, int[] pIndices, Type vType, DataContainer[] toDataContainers, DataContainer[] fromDataContainers, IValueConverter chainedConverter)
{
_converter = converter;
_convertBack = convertBack;
_values = values;
_toDataContainers = toDataContainers;
_fromDataContainers = fromDataContainers;
ConvertExpression = convertExp;
ConvertBackExpression = convertBackExp;
PTypes = pTypes;
this._pIndices = pIndices;
ValueType = vType;
_chainedConverter = chainedConverter;
}
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
for (int i = 0; i < values.Length; ++i)
{
if (values[i] == DependencyProperty.UnsetValue || _namedObjectType.IsInstanceOfType(values[i]) || (PTypes[i] != null && !PTypes[i].IsInstanceOfType(values[i])))
return DependencyProperty.UnsetValue;
}
object result;
try { result = _converter(values, _values); }
catch (Exception e)
{
LastException = e;
++ExceptionCount;
if (Debugger.IsAttached)
Console.WriteLine("QuickMultiConverter Exception (\"" + ConvertExpression + "\") - " + e.Message + (e.InnerException != null ? " (Inner - " + e.InnerException.Message + ")" : ""));
EquationTokenizer.ThrowQuickConverterEvent(new RuntimeMultiConvertExceptionEventArgs(ConvertExpression, ConvertExpressionDebugView, values, _pIndices, null, _values, parameter, this, e));
return DependencyProperty.UnsetValue;
}
finally
{
if (_toDataContainers != null)
{
foreach (var container in _toDataContainers)
container.Value = null;
}
}
if (result == DependencyProperty.UnsetValue || result == System.Windows.Data.Binding.DoNothing)
return result;
if (_chainedConverter != null)
{
try { result = _chainedConverter.Convert(result, targetType, parameter, culture); }
catch (Exception e)
{
EquationTokenizer.ThrowQuickConverterEvent(new ChainedConverterExceptionEventArgs(ConvertExpression, result, targetType, parameter, culture, false, _chainedConverter, this, e));
return DependencyProperty.UnsetValue;
}
}
result = CastResult(result, targetType);
return result;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
if (_chainedConverter != null)
{
try { value = _chainedConverter.ConvertBack(value, typeof(object), parameter, culture); }
catch (Exception e)
{
EquationTokenizer.ThrowQuickConverterEvent(new ChainedConverterExceptionEventArgs(ConvertExpression, value, typeof(object), parameter, culture, true, _chainedConverter, this, e));
return new object[targetTypes.Length].Select(o => value).ToArray();
}
if (value == DependencyProperty.UnsetValue || value == System.Windows.Data.Binding.DoNothing)
return new object[targetTypes.Length].Select(o => value).ToArray();
}
object[] ret = new object[_convertBack.Length];
if (ValueType != null && !ValueType.IsInstanceOfType(value))
{
for (int i = 0; i < ret.Length; ++i)
ret[i] = System.Windows.Data.Binding.DoNothing;
return ret;
}
for (int i = 0; i < _convertBack.Length; ++i)
{
try { ret[i] = _convertBack[i](value, _values); }
catch (Exception e)
{
LastException = e;
++ExceptionCount;
if (Debugger.IsAttached)
Console.WriteLine("QuickMultiConverter Exception (\"" + ConvertBackExpression[i] + "\") - " + e.Message + (e.InnerException != null ? " (Inner - " + e.InnerException.Message + ")" : ""));
EquationTokenizer.ThrowQuickConverterEvent(new RuntimeMultiConvertExceptionEventArgs(ConvertBackExpression[i], ConvertBackExpressionDebugView[i], null, _pIndices, value, _values, parameter, this, e));
ret[i] = DependencyProperty.UnsetValue;
}
ret[i] = CastResult(ret[i], targetTypes[i]);
}
if (_fromDataContainers != null)
{
foreach (var container in _fromDataContainers)
container.Value = null;
}
return ret;
}
private object CastResult(object result, Type targetType)
{
if (result == null || result == DependencyProperty.UnsetValue || result == System.Windows.Data.Binding.DoNothing || targetType == null || targetType == typeof(object))
return result;
if (targetType == typeof(string))
return result.ToString();
Func<object, object> cast;
if (!castFunctions.TryGetValue(targetType, out cast))
{
ParameterExpression par = Expression.Parameter(typeof(object));
cast = Expression.Lambda<Func<object, object>>(Expression.Convert(Expression.Dynamic(Binder.Convert(CSharpBinderFlags.ConvertExplicit, targetType, typeof(object)), targetType, par), typeof(object)), par).Compile();
castFunctions.TryAdd(targetType, cast);
}
if (cast != null)
{
try
{
result = cast(result);
}
catch
{
castFunctions[targetType] = null;
}
}
return result;
}
}
}