@@ -27,6 +27,11 @@ internal abstract class HostItemBase : IReflect
2727 /// </summary>
2828 protected readonly JsEngineMode _engineMode ;
2929
30+ /// <summary>
31+ /// Flag for whether to allow the usage of reflection API in the script code
32+ /// </summary>
33+ protected readonly bool _allowReflection ;
34+
3035 /// <summary>
3136 /// List of fields
3237 /// </summary>
@@ -57,20 +62,26 @@ public object Target
5762 /// <param name="type">Target type</param>
5863 /// <param name="target">Target object</param>
5964 /// <param name="engineMode">JS engine mode</param>
65+ /// <param name="allowReflection">Flag for whether to allow the usage of reflection API in the script code</param>
6066 /// <param name="instance">Flag for whether to allow access to members of the instance</param>
61- protected HostItemBase ( Type type , object target , JsEngineMode engineMode , bool instance )
67+ protected HostItemBase ( Type type , object target , JsEngineMode engineMode , bool allowReflection , bool instance )
6268 {
6369 _type = type ;
6470 _target = target ;
71+ _allowReflection = allowReflection ;
6572 _engineMode = engineMode ;
6673
6774 BindingFlags defaultBindingFlags = ReflectionHelpers . GetDefaultBindingFlags ( instance ) ;
6875 FieldInfo [ ] fields = _type . GetFields ( defaultBindingFlags ) ;
6976 PropertyInfo [ ] properties = _type . GetProperties ( defaultBindingFlags ) ;
77+ if ( properties . Length > 0 && ! allowReflection )
78+ {
79+ properties = GetAvailableProperties ( properties ) ;
80+ }
7081 MethodInfo [ ] methods = _type . GetMethods ( defaultBindingFlags ) ;
71- if ( methods . Length > 0 && properties . Length > 0 )
82+ if ( methods . Length > 0 && ( properties . Length > 0 || ! allowReflection ) )
7283 {
73- methods = ReflectionHelpers . GetFullyFledgedMethods ( methods ) ;
84+ methods = GetAvailableMethods ( methods , allowReflection ) ;
7485 }
7586
7687 _fields = fields ;
@@ -79,6 +90,49 @@ protected HostItemBase(Type type, object target, JsEngineMode engineMode, bool i
7990 }
8091
8192
93+ private static PropertyInfo [ ] GetAvailableProperties ( PropertyInfo [ ] properties )
94+ {
95+ int propertyCount = properties . Length ;
96+ var availableProperties = new PropertyInfo [ propertyCount ] ;
97+ int availablePropertyIndex = 0 ;
98+
99+ for ( int propertyIndex = 0 ; propertyIndex < propertyCount ; propertyIndex ++ )
100+ {
101+ PropertyInfo property = properties [ propertyIndex ] ;
102+ if ( ReflectionHelpers . IsAllowedProperty ( property ) )
103+ {
104+ availableProperties [ availablePropertyIndex ] = property ;
105+ availablePropertyIndex ++ ;
106+ }
107+ }
108+
109+ Array . Resize ( ref availableProperties , availablePropertyIndex ) ;
110+
111+ return availableProperties ;
112+ }
113+
114+ private static MethodInfo [ ] GetAvailableMethods ( MethodInfo [ ] methods , bool allowReflection )
115+ {
116+ int methodCount = methods . Length ;
117+ var availableMethods = new MethodInfo [ methodCount ] ;
118+ int availableMethodIndex = 0 ;
119+
120+ for ( int methodIndex = 0 ; methodIndex < methodCount ; methodIndex ++ )
121+ {
122+ MethodInfo method = methods [ methodIndex ] ;
123+ if ( ReflectionHelpers . IsFullyFledgedMethod ( method )
124+ && ( allowReflection || ReflectionHelpers . IsAllowedMethod ( method ) ) )
125+ {
126+ availableMethods [ availableMethodIndex ] = method ;
127+ availableMethodIndex ++ ;
128+ }
129+ }
130+
131+ Array . Resize ( ref availableMethods , availableMethodIndex ) ;
132+
133+ return availableMethods ;
134+ }
135+
82136 private bool IsField ( string name )
83137 {
84138 bool isField = false ;
0 commit comments