-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathDispatchWrappers.cs
More file actions
229 lines (188 loc) · 6.88 KB
/
DispatchWrappers.cs
File metadata and controls
229 lines (188 loc) · 6.88 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Microsoft.ClearScript.Util.COM
{
internal sealed class DynamicDispatchWrapper : IDynamic
{
private readonly HostItem hostItem;
private readonly IDispatch dispatch;
private IReadOnlyList<DispatchMember> members;
public DynamicDispatchWrapper(HostItem hostItem, IDispatch dispatch)
{
this.hostItem = hostItem;
this.dispatch = dispatch;
}
private IReadOnlyList<DispatchMember> GetMembers()
{
return members ?? (members = dispatch.GetMembers().ToArray());
}
public object GetProperty(string name, params object[] args)
{
return GetProperty(name, out _, args);
}
public object GetProperty(string name, out bool isCacheable, params object[] args)
{
isCacheable = false;
DispatchMember member = null;
if (args.Length < 1)
{
// some objects crash on attempt to retrieve a method as a property
member = GetMembers().FirstOrDefault(testMember => testMember.Name == name);
if ((member is not null) && member.DispatchFlags == DispatchFlags.Method)
{
return new HostMethod(hostItem, name);
}
}
try
{
var result = dispatch.GetProperty(name, args);
return result;
}
catch
{
if (args.Length < 1)
{
if (member is null)
{
member = GetMembers().FirstOrDefault(testMember => testMember.Name == name);
}
if ((member is not null) && !member.DispatchFlags.HasAllFlags(DispatchFlags.Method))
{
return new HostIndexedProperty(hostItem, name);
}
return new HostMethod(hostItem, name);
}
throw;
}
}
public void SetProperty(string name, params object[] args)
{
dispatch.SetProperty(name, args);
}
public bool DeleteProperty(string name)
{
throw new NotSupportedException("The object does not support dynamic properties");
}
public string[] GetPropertyNames()
{
return GetMembers().Select(member => member.Name).ExcludeIndices().ToArray();
}
public object GetProperty(int index)
{
return dispatch.GetProperty(index.ToString(CultureInfo.InvariantCulture));
}
public void SetProperty(int index, object value)
{
dispatch.SetProperty(index.ToString(CultureInfo.InvariantCulture), value);
}
public bool DeleteProperty(int index)
{
throw new NotSupportedException("The object does not support dynamic properties");
}
public int[] GetPropertyIndices()
{
return GetMembers().Select(member => member.Name).GetIndices().ToArray();
}
public object Invoke(bool asConstructor, params object[] args)
{
if (asConstructor)
{
throw new NotSupportedException("The object does not support constructor invocation");
}
return dispatch.Invoke(args);
}
public object InvokeMethod(string name, params object[] args)
{
return dispatch.InvokeMethod(name, args);
}
}
internal sealed class DynamicDispatchExWrapper : IDynamic
{
private readonly HostItem hostItem;
private readonly IDispatchEx dispatchEx;
public DynamicDispatchExWrapper(HostItem hostItem, IDispatchEx dispatchEx)
{
this.hostItem = hostItem;
this.dispatchEx = dispatchEx;
}
public object GetProperty(string name, params object[] args)
{
return GetProperty(name, out _, args);
}
public object GetProperty(string name, out bool isCacheable, params object[] args)
{
isCacheable = false;
DispatchMember member = null;
if (args.Length < 1)
{
// some objects crash on attempt to retrieve a method as a property
member = dispatchEx.GetMembers().FirstOrDefault(testMember => testMember.Name == name);
if ((member is not null) && member.DispatchFlags == DispatchFlags.Method)
{
return new HostMethod(hostItem, name);
}
}
try
{
var result = dispatchEx.GetProperty(name, false, args);
return result;
}
catch
{
if (args.Length < 1)
{
if (member is null)
{
member = dispatchEx.GetMembers().FirstOrDefault(testMember => testMember.Name == name);
}
if ((member is not null) && !member.DispatchFlags.HasAllFlags(DispatchFlags.Method))
{
return new HostIndexedProperty(hostItem, name);
}
return new HostMethod(hostItem, name);
}
throw;
}
}
public void SetProperty(string name, params object[] args)
{
dispatchEx.SetProperty(name, false, args);
}
public bool DeleteProperty(string name)
{
return dispatchEx.DeleteProperty(name, false);
}
public string[] GetPropertyNames()
{
return dispatchEx.GetPropertyNames().ExcludeIndices().ToArray();
}
public object GetProperty(int index)
{
return dispatchEx.GetProperty(index.ToString(CultureInfo.InvariantCulture), false);
}
public void SetProperty(int index, object value)
{
dispatchEx.SetProperty(index.ToString(CultureInfo.InvariantCulture), false, value);
}
public bool DeleteProperty(int index)
{
return dispatchEx.DeleteProperty(index.ToString(CultureInfo.InvariantCulture), false);
}
public int[] GetPropertyIndices()
{
return dispatchEx.GetPropertyNames().GetIndices().ToArray();
}
public object Invoke(bool asConstructor, params object[] args)
{
return dispatchEx.Invoke(asConstructor, args);
}
public object InvokeMethod(string name, params object[] args)
{
return dispatchEx.InvokeMethod(name, false, args);
}
}
}