@@ -53,12 +53,14 @@ public void CreateValueConverterInstances<T>(int capacity, WarmupType warmupType
53
53
public TProperty GetProperty < TProperty , TValueType > ( IBindingContext context , BindingData bindingData ,
54
54
MemberInfo memberInfo )
55
55
{
56
+ var property = GetMemberValue ( context , memberInfo , out var propertyType ) ;
57
+
56
58
var targetType = typeof ( TValueType ) ;
57
- var contextProperty = GetMemberValue ( context , memberInfo , out var sourceType ) ;
59
+ var sourceType = propertyType . GenericTypeArguments [ 0 ] ;
58
60
59
- if ( targetType == sourceType )
61
+ if ( targetType == sourceType && string . IsNullOrWhiteSpace ( bindingData . ConverterName ) )
60
62
{
61
- return ( TProperty ) contextProperty ;
63
+ return ( TProperty ) property ;
62
64
}
63
65
64
66
var converterId =
@@ -71,7 +73,7 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
71
73
return ( TProperty ) propertyWrappers
72
74
. Dequeue ( )
73
75
. AsPropertyWrapper ( )
74
- . SetProperty ( contextProperty ) ;
76
+ . SetProperty ( property ) ;
75
77
}
76
78
}
77
79
else
@@ -88,24 +90,35 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
88
90
var args = new object [ ] { valueConverter } ;
89
91
var wrapperType = typeof ( PropertyWrapper < , > ) . MakeGenericType ( sourceType , targetType ) ;
90
92
91
- return ( TProperty ) ObjectWrapperHelper . CreatePropertyWrapper ( wrapperType , args , converterId ,
92
- contextProperty ) ;
93
+ return ( TProperty ) ObjectWrapperHelper . CreatePropertyWrapper ( wrapperType , args , converterId , property ) ;
94
+ }
95
+
96
+ public TCommand GetCommand < TCommand > ( IBindingContext context , MemberInfo memberInfo )
97
+ {
98
+ var command = GetMemberValue ( context , memberInfo , out var commandType ) ;
99
+
100
+ if ( typeof ( TCommand ) . IsAssignableFrom ( commandType ) )
101
+ {
102
+ return ( TCommand ) command ;
103
+ }
104
+
105
+ throw new InvalidCastException (
106
+ $ "Can not cast the '{ commandType } ' command to the '{ typeof ( TCommand ) } ' command.") ;
93
107
}
94
108
95
109
public ICommandWrapper GetCommandWrapper ( IBindingContext context , CommandBindingData bindingData ,
96
110
MemberInfo memberInfo )
97
111
{
98
- var propertyInfo = ( PropertyInfo ) memberInfo ;
99
- var propertyType = propertyInfo . PropertyType ;
112
+ var command = GetMemberValue ( context , memberInfo , out var commandType ) ;
100
113
101
- if ( propertyType . IsGenericType == false ||
102
- propertyType . GetInterface ( nameof ( IBaseCommand ) ) == null )
114
+ if ( commandType . IsGenericType == false ||
115
+ commandType . GetInterface ( nameof ( IBaseCommand ) ) == null )
103
116
{
104
117
throw new InvalidCastException (
105
- $ "Can not cast the { propertyType } command to the { typeof ( ICommand < > ) } command.") ;
118
+ $ "Can not cast the ' { commandType } ' command to the ' { typeof ( ICommand < > ) } ' command.") ;
106
119
}
107
120
108
- var commandValueType = propertyType . GenericTypeArguments [ 0 ] ;
121
+ var commandValueType = commandType . GenericTypeArguments [ 0 ] ;
109
122
110
123
var commandId =
111
124
HashCodeHelper . GetCommandWrapperId ( context . GetType ( ) , commandValueType , bindingData . PropertyName ) ;
@@ -126,7 +139,7 @@ public ICommandWrapper GetCommandWrapper(IBindingContext context, CommandBinding
126
139
return commandWrappers
127
140
. Dequeue ( )
128
141
. AsCommandWrapper ( )
129
- . SetCommand ( commandId , propertyInfo . GetValue ( context ) )
142
+ . SetCommand ( commandId , command )
130
143
. RegisterParameter ( bindingData . ElementId , bindingData . ParameterValue ) ;
131
144
}
132
145
}
@@ -143,7 +156,6 @@ public ICommandWrapper GetCommandWrapper(IBindingContext context, CommandBinding
143
156
144
157
var args = new object [ ] { valueConverter } ;
145
158
var wrapperType = typeof ( CommandWrapper < > ) . MakeGenericType ( commandValueType ) ;
146
- var command = propertyInfo . GetValue ( context ) ;
147
159
148
160
commandWrapper = ObjectWrapperHelper
149
161
. CreateCommandWrapper ( wrapperType , args , converterId , commandId , command )
@@ -297,24 +309,26 @@ private void ReturnWrapper(IObjectWrapper wrapper)
297
309
}
298
310
299
311
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
300
- private object GetMemberValue ( IBindingContext context , MemberInfo memberInfo , out Type memberValueType )
312
+ private static object GetMemberValue ( IBindingContext context , MemberInfo memberInfo , out Type memberType )
301
313
{
302
314
switch ( memberInfo . MemberType )
303
315
{
304
316
case MemberTypes . Field :
305
317
{
306
318
var fieldInfo = ( FieldInfo ) memberInfo ;
307
- memberValueType = fieldInfo . FieldType . GenericTypeArguments [ 0 ] ;
319
+ memberType = fieldInfo . FieldType ;
308
320
309
321
return fieldInfo . GetValue ( context ) ;
310
322
}
323
+
311
324
case MemberTypes . Property :
312
325
{
313
326
var propertyInfo = ( PropertyInfo ) memberInfo ;
314
- memberValueType = propertyInfo . PropertyType . GenericTypeArguments [ 0 ] ;
327
+ memberType = propertyInfo . PropertyType ;
315
328
316
329
return propertyInfo . GetValue ( context ) ;
317
330
}
331
+
318
332
default :
319
333
throw new ArgumentOutOfRangeException ( ) ;
320
334
}
0 commit comments