@@ -12,7 +12,7 @@ public PropertyInfoAccessor(PropertyInfo propertyInfo)
12
12
{
13
13
Delegate d ;
14
14
15
- if ( ! propertyInfo . DeclaringType . IsValueType )
15
+ if ( ! propertyInfo . DeclaringType ? . IsValueType == true )
16
16
{
17
17
var delegateType = typeof ( Func < , > ) . MakeGenericType ( propertyInfo . DeclaringType , propertyInfo . PropertyType ) ;
18
18
d = propertyInfo . GetGetMethod ( ) . CreateDelegate ( delegateType ) ;
@@ -31,102 +31,77 @@ public PropertyInfoAccessor(PropertyInfo propertyInfo)
31
31
_invoker = null ;
32
32
}
33
33
34
+ Type invokerType ;
34
35
if ( propertyInfo . PropertyType == typeof ( bool ) )
35
36
{
36
- var invokerType = typeof ( BooleanInvoker < > ) . MakeGenericType ( propertyInfo . DeclaringType ) ;
37
- _invoker = ( Invoker ) Activator . CreateInstance ( invokerType , [ d ] ) ;
37
+ invokerType = typeof ( BooleanInvoker < > ) . MakeGenericType ( propertyInfo . DeclaringType ) ;
38
38
}
39
39
else if ( propertyInfo . PropertyType == typeof ( int ) )
40
40
{
41
- var invokerType = typeof ( Int32Invoker < > ) . MakeGenericType ( propertyInfo . DeclaringType ) ;
42
- _invoker = ( Invoker ) Activator . CreateInstance ( invokerType , [ d ] ) ;
41
+ invokerType = typeof ( Int32Invoker < > ) . MakeGenericType ( propertyInfo . DeclaringType ) ;
43
42
}
44
43
else
45
44
{
46
- var invokerType = typeof ( Invoker < , > ) . MakeGenericType ( propertyInfo . DeclaringType , propertyInfo . PropertyType ) ;
47
- _invoker = ( Invoker ) Activator . CreateInstance ( invokerType , [ d ] ) ;
45
+ invokerType = typeof ( Invoker < , > ) . MakeGenericType ( propertyInfo . DeclaringType , propertyInfo . PropertyType ) ;
48
46
}
49
- }
50
47
51
- public object Get ( object obj , string name , TemplateContext ctx )
52
- {
53
- return _invoker ? . Invoke ( obj ) ;
48
+ _invoker = ( Invoker ) Activator . CreateInstance ( invokerType , [ d ] ) ;
54
49
}
55
50
51
+ public object Get ( object obj , string name , TemplateContext ctx ) => _invoker ? . Invoke ( obj ) ;
52
+
56
53
private static Delegate GetGetter ( Type declaringType , string fieldName )
57
54
{
58
- string [ ] names = [ fieldName . ToLowerInvariant ( ) , $ "<{ fieldName } >k__BackingField", "_" + fieldName . ToLowerInvariant ( ) ] ;
55
+ string [ ] names = [ fieldName . ToLowerInvariant ( ) , $ "<{ fieldName } >k__BackingField", $ "_ { fieldName . ToLowerInvariant ( ) } " ] ;
59
56
60
- var field = names
61
- . Select ( n => declaringType . GetField ( n , BindingFlags . Instance | BindingFlags . NonPublic ) )
62
- . FirstOrDefault ( x => x != null ) ;
63
-
64
- if ( field == null )
57
+ FieldInfo field = null ;
58
+ foreach ( var n in names )
65
59
{
66
- return null ;
67
- }
60
+ var x = declaringType . GetField ( n , BindingFlags . Instance | BindingFlags . NonPublic ) ;
61
+ if ( x == null )
62
+ {
63
+ continue ;
64
+ }
68
65
69
- var parameterTypes = new [ ] { typeof ( object ) , declaringType } ;
66
+ var parameterTypes = new [ ] { typeof ( object ) , declaringType } ;
70
67
71
- var method = new DynamicMethod ( fieldName + "Get" , field . FieldType , parameterTypes , typeof ( PropertyInfoAccessor ) . Module , true ) ;
68
+ var method = new DynamicMethod ( fieldName + "Get" , field . FieldType , parameterTypes , typeof ( PropertyInfoAccessor ) . Module , true ) ;
72
69
73
- var emitter = method . GetILGenerator ( ) ;
74
- emitter . Emit ( OpCodes . Ldarg_1 ) ;
75
- emitter . Emit ( OpCodes . Ldfld , field ) ;
76
- emitter . Emit ( OpCodes . Ret ) ;
70
+ var emitter = method . GetILGenerator ( ) ;
71
+ emitter . Emit ( OpCodes . Ldarg_1 ) ;
72
+ emitter . Emit ( OpCodes . Ldfld , field ) ;
73
+ emitter . Emit ( OpCodes . Ret ) ;
77
74
78
- return method . CreateDelegate ( typeof ( Func < , > ) . MakeGenericType ( declaringType , field . FieldType ) ) ;
75
+ return method . CreateDelegate ( typeof ( Func < , > ) . MakeGenericType ( declaringType , field . FieldType ) ) ;
76
+ }
77
+
78
+ return null ;
79
79
}
80
80
81
81
private abstract class Invoker
82
82
{
83
83
public abstract object Invoke ( object target ) ;
84
84
}
85
85
86
- private sealed class Invoker < T , TResult > : Invoker
86
+ private sealed class Invoker < T , TResult > ( Delegate d ) : Invoker
87
87
{
88
- private readonly Func < T , TResult > _d ;
88
+ private readonly Func < T , TResult > _d = ( Func < T , TResult > ) d ;
89
89
90
- public Invoker ( Delegate d )
91
- {
92
- _d = ( Func < T , TResult > ) d ;
93
- }
94
-
95
- public override object Invoke ( object target )
96
- {
97
- return _d ( ( T ) target ) ;
98
- }
90
+ public override object Invoke ( object target ) => _d ( ( T ) target ) ;
99
91
}
100
92
101
- private sealed class BooleanInvoker < T > : Invoker
93
+ private sealed class BooleanInvoker < T > ( Delegate d ) : Invoker
102
94
{
103
- private readonly Func < T , bool > _d ;
104
-
105
- public BooleanInvoker ( Delegate d )
106
- {
107
- _d = ( Func < T , bool > ) d ;
108
- }
95
+ private readonly Func < T , bool > _d = ( Func < T , bool > ) d ;
109
96
110
- public override object Invoke ( object target )
111
- {
112
- return _d ( ( T ) target ) ? BooleanValue . True : BooleanValue . False ;
113
- }
97
+ public override object Invoke ( object target ) => _d ( ( T ) target ) ? BooleanValue . True : BooleanValue . False ;
114
98
}
115
99
116
- private sealed class Int32Invoker < T > : Invoker
100
+ private sealed class Int32Invoker < T > ( Delegate d ) : Invoker
117
101
{
118
- private readonly Func < T , int > _d ;
102
+ private readonly Func < T , int > _d = ( Func < T , int > ) d ;
119
103
120
- public Int32Invoker ( Delegate d )
121
- {
122
- _d = ( Func < T , int > ) d ;
123
- }
124
-
125
- public override object Invoke ( object target )
126
- {
127
- var value = _d ( ( T ) target ) ;
128
- return NumberValue . Create ( value ) ;
129
- }
104
+ public override object Invoke ( object target ) => NumberValue . Create ( _d ( ( T ) target ) ) ;
130
105
}
131
106
}
132
107
}
0 commit comments