forked from Taritsyn/MsieJavaScriptEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostItemBase.cs
More file actions
203 lines (168 loc) · 5.4 KB
/
HostItemBase.cs
File metadata and controls
203 lines (168 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#if !NETSTANDARD
using System;
using System.Globalization;
using System.Reflection;
using MsieJavaScriptEngine.Helpers;
namespace MsieJavaScriptEngine
{
/// <summary>
/// Base class of item, that implements <see cref="IReflect"/> interface
/// </summary>
internal abstract class HostItemBase : IReflect
{
/// <summary>
/// Target type
/// </summary>
protected readonly Type _type;
/// <summary>
/// Target object
/// </summary>
protected readonly object _target;
/// <summary>
/// JS engine mode
/// </summary>
protected readonly JsEngineMode _engineMode;
/// <summary>
/// List of fields
/// </summary>
private readonly FieldInfo[] _fields;
/// <summary>
/// List of properties
/// </summary>
private readonly PropertyInfo[] _properties;
/// <summary>
/// List of methods
/// </summary>
private readonly MethodInfo[] _methods;
/// <summary>
/// Gets a target object
/// </summary>
public object Target
{
get { return _target; }
}
/// <summary>
/// Constructs an instance of the wrapper for item, that implements <see cref="IReflect"/> interface
/// </summary>
/// <param name="type">Target type</param>
/// <param name="target">Target object</param>
/// <param name="engineMode">JS engine mode</param>
/// <param name="instance">Flag for whether to allow access to members of the instance</param>
protected HostItemBase(Type type, object target, JsEngineMode engineMode, bool instance)
{
_type = type;
_target = target;
_engineMode = engineMode;
BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);
FieldInfo[] fields = _type.GetFields(defaultBindingFlags);
PropertyInfo[] properties = _type.GetProperties(defaultBindingFlags);
MethodInfo[] methods = _type.GetMethods(defaultBindingFlags);
if (methods.Length > 0 && properties.Length > 0)
{
methods = ReflectionHelpers.GetFullyFledgedMethods(methods);
}
_fields = fields;
_properties = properties;
_methods = methods;
}
private bool IsField(string name)
{
bool isField = false;
FieldInfo[] fields = _fields;
int fieldCount = fields.Length;
for (int fieldIndex = 0; fieldIndex < fieldCount; fieldIndex++)
{
if (fields[fieldIndex].Name.Equals(name, StringComparison.Ordinal))
{
isField = true;
break;
}
}
return isField;
}
protected abstract object InnerInvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target,
object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters);
protected object InvokeStandardMember(string name, BindingFlags invokeAttr, Binder binder, object target,
object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
BindingFlags processedInvokeAttr = invokeAttr;
if ((processedInvokeAttr.HasFlag(BindingFlags.GetProperty)
|| processedInvokeAttr.HasFlag(BindingFlags.SetProperty)
|| processedInvokeAttr.HasFlag(BindingFlags.PutDispProperty))
&& IsField(name))
{
if (processedInvokeAttr.HasFlag(BindingFlags.GetProperty))
{
processedInvokeAttr &= ~BindingFlags.GetProperty;
processedInvokeAttr |= BindingFlags.GetField;
}
else if (processedInvokeAttr.HasFlag(BindingFlags.SetProperty))
{
processedInvokeAttr &= ~BindingFlags.SetProperty;
processedInvokeAttr |= BindingFlags.SetField;
}
else if (processedInvokeAttr.HasFlag(BindingFlags.PutDispProperty))
{
processedInvokeAttr &= ~BindingFlags.PutDispProperty;
processedInvokeAttr |= BindingFlags.SetField;
}
}
object result = _type.InvokeMember(name, processedInvokeAttr, binder, target,
args, modifiers, culture, namedParameters);
return result;
}
#region IReflect implementation
Type IReflect.UnderlyingSystemType
{
get { throw new NotImplementedException(); }
}
FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
{
return _fields;
}
MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
{
throw new NotImplementedException();
}
MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
{
return _methods;
}
PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
{
return _properties;
}
PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
{
throw new NotImplementedException();
}
PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder,
Type returnType, Type[] types, ParameterModifier[] modifiers)
{
throw new NotImplementedException();
}
object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target,
object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
return InnerInvokeMember(name, invokeAttr, binder,target, args, modifiers, culture, namedParameters);
}
#endregion
}
}
#endif