-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathObjectHelpers.cs
More file actions
358 lines (295 loc) · 11.1 KB
/
ObjectHelpers.cs
File metadata and controls
358 lines (295 loc) · 11.1 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.ClearScript.Util.COM;
using TYPEFLAGS = System.Runtime.InteropServices.ComTypes.TYPEFLAGS;
using TYPEKIND = System.Runtime.InteropServices.ComTypes.TYPEKIND;
namespace Microsoft.ClearScript.Util
{
internal static partial class ObjectHelpers
{
private static readonly object[] zeroes =
{
(sbyte)0,
(byte)0,
(short)0,
(ushort)0,
0,
0U,
0L,
0UL,
IntPtr.Zero,
UIntPtr.Zero,
0.0f,
0.0d,
0.0m
};
public static bool IsZero(this object value) => Array.IndexOf(zeroes, value) >= 0;
public static bool IsWholeNumber(this object value)
{
// ReSharper disable CompareOfFloatsByEqualityOperator
if (value is null)
{
return false;
}
if (value.GetType().IsIntegral())
{
return true;
}
if (value is float floatValue)
{
return Math.Truncate(floatValue) == floatValue;
}
if (value is double doubleValue)
{
return Math.Truncate(doubleValue) == doubleValue;
}
if (value is decimal decimalValue)
{
return Math.Truncate(decimalValue) == decimalValue;
}
return false;
// ReSharper restore CompareOfFloatsByEqualityOperator
}
public static Type GetTypeOrTypeInfo(this object value)
{
if (!MiscHelpers.PlatformIsWindows())
{
return value.GetType();
}
var type = value.GetType();
IDispatch dispatch = null;
Type typeInfo = null;
TYPEKIND typeInfoKind = 0;
TYPEFLAGS typeInfoFlags = 0;
if (type.IsUnknownCOMObject())
{
// This appears to be a generic COM object with no specific type information.
// Attempt to acquire COM type information via IDispatch or IProvideClassInfo.
dispatch = value as IDispatch;
if (dispatch is not null)
{
var tempTypeInfo = dispatch.GetTypeInfo();
if (tempTypeInfo is not null)
{
typeInfo = GetTypeForTypeInfo(tempTypeInfo);
typeInfoKind = tempTypeInfo.GetKind();
typeInfoFlags = tempTypeInfo.GetFlags();
}
}
if (typeInfo is null)
{
if (value is IProvideClassInfo provideClassInfo)
{
if (HResult.Succeeded(provideClassInfo.GetClassInfo(out var tempTypeInfo)))
{
typeInfo = GetTypeForTypeInfo(tempTypeInfo);
typeInfoKind = tempTypeInfo.GetKind();
typeInfoFlags = tempTypeInfo.GetFlags();
}
}
}
}
if (typeInfo is not null)
{
// If the COM type is a dispatch-only interface, use it. Such interfaces typically
// aren't exposed via QueryInterface(), so there's no way to validate them anyway.
if ((dispatch is not null) && (typeInfoKind == TYPEKIND.TKIND_DISPATCH) && typeInfoFlags.HasAllFlags(TYPEFLAGS.TYPEFLAG_FDISPATCHABLE) && !typeInfoFlags.HasAllFlags(TYPEFLAGS.TYPEFLAG_FDUAL))
{
return typeInfo;
}
// COM type information acquired in this manner may not actually be valid for the
// original object. In some cases the original object implements a base interface.
if (typeInfo.IsInstanceOfType(value))
{
return typeInfo;
}
foreach (var interfaceType in typeInfo.GetInterfaces())
{
if (interfaceType.IsInstanceOfType(value))
{
return interfaceType;
}
}
}
return type;
}
private static Type GetTypeForTypeInfo(ITypeInfo typeInfo)
{
// ReSharper disable EmptyGeneralCatchClause
try
{
var typeLib = typeInfo.GetContainingTypeLib(out var index);
var assembly = LoadPrimaryInteropAssembly(typeLib);
if (assembly is not null)
{
var name = typeInfo.GetManagedName();
var guid = typeInfo.GetGuid();
var type = assembly.GetType(name, false, true);
if ((type is not null) && (type.GUID == guid))
{
return type;
}
var types = assembly.GetAllTypes().ToArray();
if ((index >= 0) && (index < types.Length))
{
type = types[index];
if ((type.GUID == guid) && (type.FullName == name))
{
return type;
}
}
type = types.FirstOrDefault(testType => (testType.GUID == guid) && (testType.FullName.Equals(name, StringComparison.OrdinalIgnoreCase)));
if (type is not null)
{
return type;
}
}
return typeInfo.GetManagedType();
}
catch
{
}
return null;
// ReSharper restore EmptyGeneralCatchClause
}
private static Assembly LoadPrimaryInteropAssembly(ITypeLib typeLib)
{
if (typeLib is null)
{
return null;
}
// ReSharper disable EmptyGeneralCatchClause
try
{
using (var attrScope = typeLib.CreateAttrScope())
{
if (GetPrimaryInteropAssembly(attrScope.Value.guid, attrScope.Value.wMajorVerNum, attrScope.Value.wMinorVerNum, out var name, out var codeBase))
{
return Assembly.Load(new AssemblyName(name) { CodeBase = codeBase });
}
}
}
catch
{
}
return null;
// ReSharper restore EmptyGeneralCatchClause
}
public static string GetFriendlyName(this object value)
{
return value.GetFriendlyName(null);
}
public static string GetFriendlyName(this object value, Type type)
{
if (type is null)
{
if (value is null)
{
return "[null]";
}
type = value.GetType();
}
if (type.IsArray && (value is not null))
{
var array = (Array)value;
var dimensions = Enumerable.Range(0, type.GetArrayRank());
var lengths = string.Join(",", dimensions.Select(array.GetLength));
return MiscHelpers.FormatInvariant("{0}[{1}]", type.GetElementType().GetFriendlyName(), lengths);
}
if (type.IsUnknownCOMObject())
{
if (value is IDispatch dispatch)
{
var typeInfo = dispatch.GetTypeInfo();
if (typeInfo is not null)
{
return typeInfo.GetName();
}
}
}
return type.GetFriendlyName();
}
public static T DynamicCast<T>(this object value)
{
return DynamicCaster<T>.Cast(value);
}
public static object ToDynamicResult(this object result, ScriptEngine engine)
{
if (result is Nonexistent)
{
return Undefined.Value;
}
if ((result is HostTarget) || (result is IPropertyBag))
{
// Returning an instance of HostTarget (an internal type) isn't likely to be
// useful. Wrapping it in a dynamic object makes sense in this context. Wrapping
// a property bag allows it to participate in dynamic invocation chaining, which
// may be useful when dealing with things like host type collections. HostItem
// supports dynamic conversion, so the client can unwrap the object if necessary.
return HostItem.Wrap(engine, result);
}
return result;
}
#region Nested type: IProvideClassInfo
[ComImport]
[Guid("b196b283-bab4-101a-b69c-00aa00341d07")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IProvideClassInfo
{
[PreserveSig]
int GetClassInfo(
[Out] [MarshalAs(UnmanagedType.Interface)] out ITypeInfo typeInfo
);
}
#endregion
#region Nested type: DynamicCaster<T>
private static class DynamicCaster<T>
{
public static T Cast(object value)
{
// ReSharper disable EmptyGeneralCatchClause
try
{
if (!typeof(T).IsValueType)
{
return (T)value;
}
if (typeof(T).IsEnum)
{
return (T)Enum.ToObject(typeof(T), value);
}
if (typeof(T).IsNullable())
{
return (T)CastToNullable(value);
}
if (value is IConvertible)
{
return (T)Convert.ChangeType(value, typeof(T));
}
return (T)value;
}
catch
{
}
return (T)(dynamic)value;
// ReSharper restore EmptyGeneralCatchClause
}
private static object CastToNullable(object value)
{
if (value is not null)
{
var valueCastType = typeof(DynamicCaster<>).MakeGenericType(Nullable.GetUnderlyingType(typeof(T)));
value = valueCastType.InvokeMember("Cast", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new[] { value });
return typeof(T).CreateInstance(value);
}
return null;
}
}
#endregion
}
}