@@ -27,6 +27,11 @@ internal abstract class HostItemBase : IReflect
27
27
/// </summary>
28
28
protected readonly JsEngineMode _engineMode ;
29
29
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
+
30
35
/// <summary>
31
36
/// List of fields
32
37
/// </summary>
@@ -57,20 +62,26 @@ public object Target
57
62
/// <param name="type">Target type</param>
58
63
/// <param name="target">Target object</param>
59
64
/// <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>
60
66
/// <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 )
62
68
{
63
69
_type = type ;
64
70
_target = target ;
71
+ _allowReflection = allowReflection ;
65
72
_engineMode = engineMode ;
66
73
67
74
BindingFlags defaultBindingFlags = ReflectionHelpers . GetDefaultBindingFlags ( instance ) ;
68
75
FieldInfo [ ] fields = _type . GetFields ( defaultBindingFlags ) ;
69
76
PropertyInfo [ ] properties = _type . GetProperties ( defaultBindingFlags ) ;
77
+ if ( properties . Length > 0 && ! allowReflection )
78
+ {
79
+ properties = GetAvailableProperties ( properties ) ;
80
+ }
70
81
MethodInfo [ ] methods = _type . GetMethods ( defaultBindingFlags ) ;
71
- if ( methods . Length > 0 && properties . Length > 0 )
82
+ if ( methods . Length > 0 && ( properties . Length > 0 || ! allowReflection ) )
72
83
{
73
- methods = ReflectionHelpers . GetFullyFledgedMethods ( methods ) ;
84
+ methods = GetAvailableMethods ( methods , allowReflection ) ;
74
85
}
75
86
76
87
_fields = fields ;
@@ -79,6 +90,49 @@ protected HostItemBase(Type type, object target, JsEngineMode engineMode, bool i
79
90
}
80
91
81
92
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
+
82
136
private bool IsField ( string name )
83
137
{
84
138
bool isField = false ;
0 commit comments