@@ -27,9 +27,11 @@ internal static class PropertyAccessor
2727 {
2828 // Validate the converter input type matches property type
2929 var converterParamType = converter . Parameters [ 0 ] . Type ;
30- if ( ! converterParamType . IsAssignableFrom ( propertyType ) && ! propertyType . IsAssignableFrom ( converterParamType ) )
30+ if ( ! converterParamType . IsAssignableFrom ( propertyType ) &&
31+ ! propertyType . IsAssignableFrom ( converterParamType ) )
3132 {
32- throw new ArgumentException ( $ "Converter input must be assignable from property type ({ propertyType } -> { converterParamType } )") ;
33+ throw new ArgumentException (
34+ $ "Converter input must be assignable from property type ({ propertyType } -> { converterParamType } )") ;
3335 }
3436
3537 // If property type != converter param, convert
@@ -39,7 +41,18 @@ internal static class PropertyAccessor
3941 converterInput = Expression . Convert ( getterExpression , converterParamType ) ;
4042 }
4143
42- getterExpression = Expression . Invoke ( converter , converterInput ) ;
44+ var invokeConverter = Expression . Invoke ( converter , converterInput ) ;
45+
46+ if ( propertyType . IsClass )
47+ {
48+ var nullCondition = Expression . Equal ( getterExpression , Expression . Constant ( null , propertyType ) ) ;
49+ var nullResult = Expression . Constant ( null , converter . ReturnType ) ;
50+ getterExpression = Expression . Condition ( nullCondition , nullResult , invokeConverter ) ;
51+ }
52+ else
53+ {
54+ getterExpression = invokeConverter ;
55+ }
4356
4457 propertyType = getterExpression . Type ;
4558 }
0 commit comments