@@ -11,9 +11,6 @@ internal sealed class PropertyGetter
1111{
1212 private static readonly MethodInfo CallPropertyGetterOpenGenericMethod =
1313 typeof ( PropertyGetter ) . GetMethod ( nameof ( CallPropertyGetter ) , BindingFlags . NonPublic | BindingFlags . Static ) ! ;
14-
15- private static readonly MethodInfo CallPropertyGetterByReferenceOpenGenericMethod =
16- typeof ( PropertyGetter ) . GetMethod ( nameof ( CallPropertyGetterByReference ) , BindingFlags . NonPublic | BindingFlags . Static ) ! ;
1714
1815 // Delegate type for a by-ref property getter
1916 private delegate TValue ByRefFunc < TDeclaringType , TValue > ( ref TDeclaringType arg ) ;
@@ -37,31 +34,14 @@ public PropertyGetter(Type targetType, PropertyInfo property)
3734 {
3835 var getMethod = property . GetMethod ;
3936
40- // Instance methods in the CLR can be turned into static methods where the first parameter
41- // is open over "target". This parameter is always passed by reference, so we have a code
42- // path for value types and a code path for reference types.
43- if ( getMethod . DeclaringType ! . IsValueType )
44- {
45- // Create a delegate (ref TDeclaringType) -> TValue
46- var delegateType = typeof ( ByRefFunc < , > ) . MakeGenericType ( targetType , property . PropertyType ) ;
47- var propertyGetterDelegate = getMethod . CreateDelegate ( delegateType ) ;
48- var wrapperDelegateMethod = CallPropertyGetterByReferenceOpenGenericMethod . MakeGenericMethod ( targetType , property . PropertyType ) ;
49- var accessorDelegate = wrapperDelegateMethod . CreateDelegate (
50- typeof ( Func < object , object ? > ) ,
51- propertyGetterDelegate ) ;
52- _GetterDelegate = ( Func < object , object ? > ) accessorDelegate ;
53- }
54- else
55- {
56- // Create a delegate TDeclaringType -> TValue
57- var delegateType = typeof ( Func < , > ) . MakeGenericType ( targetType , property . PropertyType ) ;
58- var propertyGetterDelegate = getMethod . CreateDelegate ( delegateType ) ;
59- var wrapperDelegateMethod = CallPropertyGetterOpenGenericMethod . MakeGenericMethod ( targetType , property . PropertyType ) ;
60- var accessorDelegate = wrapperDelegateMethod . CreateDelegate (
61- typeof ( Func < object , object ? > ) ,
62- propertyGetterDelegate ) ;
63- _GetterDelegate = ( Func < object , object ? > ) accessorDelegate ;
64- }
37+ var propertyGetterAsFunc =
38+ getMethod . CreateDelegate ( typeof ( Func < , > ) . MakeGenericType ( targetType , property . PropertyType ) ) ;
39+
40+ var callPropertyGetterClosedGenericMethod =
41+ CallPropertyGetterOpenGenericMethod . MakeGenericMethod ( targetType , property . PropertyType ) ;
42+
43+ _GetterDelegate = ( Func < object , object > )
44+ callPropertyGetterClosedGenericMethod . CreateDelegate ( typeof ( Func < object , object > ) , propertyGetterAsFunc ) ;
6545 }
6646 else
6747 {
@@ -78,13 +58,4 @@ public PropertyGetter(Type targetType, PropertyInfo property)
7858 {
7959 return Getter ( ( TTarget ) target ) ;
8060 }
81-
82- private static object ? CallPropertyGetterByReference < TTarget , TValue > (
83- ByRefFunc < TTarget , TValue > Getter ,
84- object target )
85- where TTarget : notnull
86- {
87- var unboxed = ( TTarget ) target ;
88- return Getter ( ref unboxed ) ;
89- }
9061}
0 commit comments