@@ -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,76 @@ 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
+ foreach ( var n in names )
65
58
{
66
- return null ;
67
- }
59
+ var field = declaringType . GetField ( n , BindingFlags . Instance | BindingFlags . NonPublic ) ;
60
+ if ( field == null )
61
+ {
62
+ continue ;
63
+ }
68
64
69
- var parameterTypes = new [ ] { typeof ( object ) , declaringType } ;
65
+ var parameterTypes = new [ ] { typeof ( object ) , declaringType } ;
70
66
71
- var method = new DynamicMethod ( fieldName + "Get" , field . FieldType , parameterTypes , typeof ( PropertyInfoAccessor ) . Module , true ) ;
67
+ var method = new DynamicMethod ( fieldName + "Get" , field . FieldType , parameterTypes , typeof ( PropertyInfoAccessor ) . Module , true ) ;
72
68
73
- var emitter = method . GetILGenerator ( ) ;
74
- emitter . Emit ( OpCodes . Ldarg_1 ) ;
75
- emitter . Emit ( OpCodes . Ldfld , field ) ;
76
- emitter . Emit ( OpCodes . Ret ) ;
69
+ var emitter = method . GetILGenerator ( ) ;
70
+ emitter . Emit ( OpCodes . Ldarg_1 ) ;
71
+ emitter . Emit ( OpCodes . Ldfld , field ) ;
72
+ emitter . Emit ( OpCodes . Ret ) ;
77
73
78
- return method . CreateDelegate ( typeof ( Func < , > ) . MakeGenericType ( declaringType , field . FieldType ) ) ;
74
+ return method . CreateDelegate ( typeof ( Func < , > ) . MakeGenericType ( declaringType , field . FieldType ) ) ;
75
+ }
76
+
77
+ return null ;
79
78
}
80
79
81
80
private abstract class Invoker
82
81
{
83
82
public abstract object Invoke ( object target ) ;
84
83
}
85
84
86
- private sealed class Invoker < T , TResult > : Invoker
85
+ private sealed class Invoker < T , TResult > ( Delegate d ) : Invoker
87
86
{
88
- private readonly Func < T , TResult > _d ;
87
+ private readonly Func < T , TResult > _d = ( Func < T , TResult > ) d ;
89
88
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
- }
89
+ public override object Invoke ( object target ) => _d ( ( T ) target ) ;
99
90
}
100
91
101
- private sealed class BooleanInvoker < T > : Invoker
92
+ private sealed class BooleanInvoker < T > ( Delegate d ) : Invoker
102
93
{
103
- private readonly Func < T , bool > _d ;
104
-
105
- public BooleanInvoker ( Delegate d )
106
- {
107
- _d = ( Func < T , bool > ) d ;
108
- }
94
+ private readonly Func < T , bool > _d = ( Func < T , bool > ) d ;
109
95
110
- public override object Invoke ( object target )
111
- {
112
- return _d ( ( T ) target ) ? BooleanValue . True : BooleanValue . False ;
113
- }
96
+ public override object Invoke ( object target ) => _d ( ( T ) target ) ? BooleanValue . True : BooleanValue . False ;
114
97
}
115
98
116
- private sealed class Int32Invoker < T > : Invoker
99
+ private sealed class Int32Invoker < T > ( Delegate d ) : Invoker
117
100
{
118
- private readonly Func < T , int > _d ;
101
+ private readonly Func < T , int > _d = ( Func < T , int > ) d ;
119
102
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
- }
103
+ public override object Invoke ( object target ) => NumberValue . Create ( _d ( ( T ) target ) ) ;
130
104
}
131
105
}
132
106
}
0 commit comments