22{
33 using System ;
44 using System . Collections . Concurrent ;
5- using System . Collections . Generic ;
6- using System . Linq ;
7- using System . Reflection ;
85 using Microsoft . AspNetCore . Mvc ;
9- using Utilities ;
106
11- public class ControllerPropertyHelper
7+ public class ControllerPropertyHelper : BasePropertyHelper
128 {
13- private const string InvalidDelegateErrorMessage = "The {0} property cannot be activated for value of {1} type." ;
14-
15- private static readonly MethodInfo CallPropertyGetterOpenGenericMethod =
16- typeof ( ControllerPropertyHelper ) . GetTypeInfo ( ) . GetDeclaredMethod ( nameof ( CallPropertyGetter ) ) ;
17-
189 private static readonly ConcurrentDictionary < Type , ControllerPropertyHelper > ControllerPropertiesCache =
1910 new ConcurrentDictionary < Type , ControllerPropertyHelper > ( ) ;
2011
21- private readonly Type controllerType ;
22-
2312 private Func < object , ControllerContext > controllerContextGetter ;
2413 private Func < object , ActionContext > actionContextGetter ;
2514
2615 public ControllerPropertyHelper ( Type controllerType )
16+ : base ( controllerType )
2717 {
28- this . controllerType = controllerType ;
29- this . Properties = controllerType . GetProperties ( BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
18+
3019 }
3120
3221 public Func < object , ControllerContext > ControllerContextGetter
@@ -55,8 +44,6 @@ public Func<object, ActionContext> ActionContextGetter
5544 }
5645 }
5746
58- protected IEnumerable < PropertyInfo > Properties { get ; private set ; }
59-
6047 public static ControllerPropertyHelper GetProperties < TController > ( )
6148 where TController : class
6249 {
@@ -68,39 +55,6 @@ public static ControllerPropertyHelper GetProperties(Type type)
6855 return ControllerPropertiesCache . GetOrAdd ( type , _ => new ControllerPropertyHelper ( type ) ) ;
6956 }
7057
71- protected static Func < object , TResult > MakeFastPropertyGetter < TResult > ( PropertyInfo propertyInfo )
72- {
73- try
74- {
75- var propertyGetMethod = propertyInfo . GetMethod ;
76-
77- var typeInput = propertyGetMethod . DeclaringType ;
78- var typeOutput = propertyGetMethod . ReturnType ;
79-
80- var delegateType = typeof ( Func < , > ) . MakeGenericType ( typeInput , typeOutput ) ;
81- var propertyGetterDelegate = propertyGetMethod . CreateDelegate ( delegateType ) ;
82-
83- var wrapperDelegateMethod = CallPropertyGetterOpenGenericMethod . MakeGenericMethod ( typeInput , typeOutput ) ;
84- var accessorDelegate = wrapperDelegateMethod . CreateDelegate (
85- typeof ( Func < object , TResult > ) ,
86- propertyGetterDelegate ) ;
87-
88- return ( Func < object , TResult > ) accessorDelegate ;
89- }
90- catch
91- {
92- throw new InvalidOperationException ( string . Format ( InvalidDelegateErrorMessage , propertyInfo . Name , typeof ( TResult ) ) ) ;
93- }
94- }
95-
96- // Called via reflection
97- private static TValue CallPropertyGetter < TDeclaringType , TValue > (
98- Func < TDeclaringType , TValue > getter ,
99- object target )
100- {
101- return getter ( ( TDeclaringType ) target ) ;
102- }
103-
10458 private void TryCreateControllerContextDelegates ( )
10559 {
10660 var controllerContextProperty = this . FindPropertyWithAttribute < ControllerContextAttribute > ( ) ;
@@ -116,19 +70,5 @@ private void TryCreateActionContextDelegates()
11670
11771 this . actionContextGetter = MakeFastPropertyGetter < ActionContext > ( actionContextProperty ) ;
11872 }
119-
120- protected PropertyInfo FindPropertyWithAttribute < TAttribute > ( )
121- where TAttribute : Attribute
122- {
123- return this . Properties . FirstOrDefault ( pr => pr . GetCustomAttribute ( typeof ( TAttribute ) , true ) != null ) ;
124- }
125-
126- protected void ThrowNewInvalidOperationExceptionIfNull ( object value , string propertyName )
127- {
128- if ( value == null )
129- {
130- throw new InvalidOperationException ( $ "{ propertyName } could not be found on the provided { controllerType . ToFriendlyTypeName ( ) } . The property should be specified manually by providing controller instance or using the specified helper methods.") ;
131- }
132- }
13373 }
13474}
0 commit comments