forked from JohannesMoersch/QuickConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicSingleConverter.cs
162 lines (142 loc) · 6.11 KB
/
DynamicSingleConverter.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
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 DynamicSingleConverter : IValueConverter
{
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 PType { get; private set; }
public Type ValueType { get; private set; }
public object[] Values { get { return _values; } }
private Func<object, object[], object, object> _converter;
private Func<object, object[], object, object> _convertBack;
private object[] _values;
private DataContainer[] _toDataContainers;
private DataContainer[] _fromDataContainers;
private IValueConverter _chainedConverter;
public DynamicSingleConverter(Func<object, object[], object, object> converter, Func<object, object[], object, object> convertBack, object[] values, string convertExp, string convertExpDebug, string convertBackExp, string convertBackExpDebug, Type pType, Type vType, DataContainer[] toDataContainers, DataContainer[] fromDataContainers, IValueConverter chainedConverter)
{
_converter = converter;
_convertBack = convertBack;
_values = values;
_toDataContainers = toDataContainers;
_fromDataContainers = fromDataContainers;
ConvertExpression = convertExp;
ConvertBackExpression = convertBackExp;
ConvertExpressionDebugView = convertExpDebug;
ConvertBackExpressionDebugView = convertBackExpDebug;
PType = pType;
ValueType = vType;
_chainedConverter = chainedConverter;
}
private object DoConversion(object value, Type targetType, object parameter, Func<object, object[], object, object> func, bool convertingBack, CultureInfo culture)
{
if (convertingBack)
{
if (_chainedConverter != null)
{
try { value = _chainedConverter.ConvertBack(value, targetType, parameter, culture); }
catch (Exception e)
{
EquationTokenizer.ThrowQuickConverterEvent(new ChainedConverterExceptionEventArgs(ConvertExpression, value, targetType, parameter, culture, true, _chainedConverter, this, e));
return DependencyProperty.UnsetValue;
}
if (value == DependencyProperty.UnsetValue || value == System.Windows.Data.Binding.DoNothing)
return value;
}
if (ValueType != null && !ValueType.IsInstanceOfType(value))
return System.Windows.Data.Binding.DoNothing;
}
else
{
if (value == DependencyProperty.UnsetValue || _namedObjectType.IsInstanceOfType(value) || (PType != null && !PType.IsInstanceOfType(value)))
return DependencyProperty.UnsetValue;
}
object result = value;
if (func != null)
{
try { result = func(result, _values, parameter); }
catch (Exception e)
{
LastException = e;
++ExceptionCount;
if (Debugger.IsAttached)
Console.WriteLine("QuickMultiConverter Exception (\"" + (convertingBack ? ConvertBackExpression : ConvertExpression) + "\") - " + e.Message + (e.InnerException != null ? " (Inner - " + e.InnerException.Message + ")" : ""));
if (convertingBack)
EquationTokenizer.ThrowQuickConverterEvent(new RuntimeSingleConvertExceptionEventArgs(ConvertBackExpression, ConvertBackExpressionDebugView, null, value, _values, parameter, this, e));
else
EquationTokenizer.ThrowQuickConverterEvent(new RuntimeSingleConvertExceptionEventArgs(ConvertExpression, ConvertExpressionDebugView, value, null, _values, parameter, this, e));
return DependencyProperty.UnsetValue;
}
finally
{
var dataContainers = convertingBack ? _fromDataContainers : _toDataContainers;
if (dataContainers != null)
{
foreach (var container in dataContainers)
container.Value = null;
}
}
}
if (result == DependencyProperty.UnsetValue || result == System.Windows.Data.Binding.DoNothing)
return result;
if (!convertingBack && _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;
}
}
if (result == DependencyProperty.UnsetValue || result == System.Windows.Data.Binding.DoNothing || result == null || 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;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return DoConversion(value, targetType, parameter, _converter, false, culture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DoConversion(value, targetType, parameter, _convertBack, true, culture);
}
}
}