-
Notifications
You must be signed in to change notification settings - Fork 82
/
Variant.cs
963 lines (829 loc) · 40.8 KB
/
Variant.cs
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#if FEATURE_COM
#pragma warning disable CA1416 // Validate platform compatibility
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Utils;
namespace Microsoft.Scripting.ComInterop {
/// <summary>
/// Variant is the basic COM type for late-binding. It can contain any other COM data type.
/// This type definition precisely matches the unmanaged data layout so that the struct can be passed
/// to and from COM calls.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
internal struct Variant {
#if DEBUG
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2207:InitializeValueTypeStaticFieldsInline")]
static Variant() {
// Variant size is the size of 4 pointers (16 bytes) on a 32-bit processor,
// and 3 pointers (24 bytes) on a 64-bit processor.
int intPtrSize = Marshal.SizeOf(typeof(IntPtr));
int variantSize = Marshal.SizeOf(typeof(Variant));
if (intPtrSize == 4) {
Debug.Assert(variantSize == (4 * intPtrSize));
} else {
Debug.Assert(intPtrSize == 8);
Debug.Assert(variantSize == (3 * intPtrSize));
}
}
#endif
// Most of the data types in the Variant are carried in _typeUnion
[FieldOffset(0)]
private TypeUnion _typeUnion;
// Decimal is the largest data type and it needs to use the space that is normally unused in TypeUnion._wReserved1, etc.
// Hence, it is declared to completely overlap with TypeUnion. A Decimal does not use the first two bytes, and so
// TypeUnion._vt can still be used to encode the type.
[FieldOffset(0)]
private Decimal _decimal;
[StructLayout(LayoutKind.Sequential)]
private struct TypeUnion {
internal ushort _vt;
internal ushort _wReserved1;
internal ushort _wReserved2;
internal ushort _wReserved3;
internal UnionTypes _unionTypes;
}
[StructLayout(LayoutKind.Sequential)]
private struct Record {
internal IntPtr _record;
internal IntPtr _recordInfo;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1049:TypesThatOwnNativeResourcesShouldBeDisposable")]
[StructLayout(LayoutKind.Explicit)]
private struct UnionTypes {
#region Generated Outer Variant union types
// *** BEGIN GENERATED CODE ***
// generated by function: gen_UnionTypes from: generate_comdispatch.py
[FieldOffset(0)] internal SByte _i1;
[FieldOffset(0)] internal Int16 _i2;
[FieldOffset(0)] internal Int32 _i4;
[FieldOffset(0)] internal Int64 _i8;
[FieldOffset(0)] internal Byte _ui1;
[FieldOffset(0)] internal UInt16 _ui2;
[FieldOffset(0)] internal UInt32 _ui4;
[FieldOffset(0)] internal UInt64 _ui8;
[SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
[FieldOffset(0)] internal IntPtr _int;
[FieldOffset(0)] internal UIntPtr _uint;
[FieldOffset(0)] internal Int16 _bool;
[FieldOffset(0)] internal Int32 _error;
[FieldOffset(0)] internal Single _r4;
[FieldOffset(0)] internal Double _r8;
[FieldOffset(0)] internal Int64 _cy;
[FieldOffset(0)] internal Double _date;
[SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
[SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
[FieldOffset(0)] internal IntPtr _bstr;
[SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
[FieldOffset(0)] internal IntPtr _unknown;
[SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
[FieldOffset(0)] internal IntPtr _dispatch;
// *** END GENERATED CODE ***
#endregion
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
[FieldOffset(0)]
internal IntPtr _byref;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
[FieldOffset(0)]
internal Record _record;
}
public override string ToString() {
return String.Format(CultureInfo.CurrentCulture, "Variant ({0})", VariantType);
}
/// <summary>
/// Primitive types are the basic COM types. It includes valuetypes like ints, but also reference types
/// like BStrs. It does not include composite types like arrays and user-defined COM types (IUnknown/IDispatch).
/// </summary>
internal static bool IsPrimitiveType(VarEnum varEnum) {
switch (varEnum) {
#region Generated Outer Variant IsPrimitiveType
// *** BEGIN GENERATED CODE ***
// generated by function: gen_IsPrimitiveType from: generate_comdispatch.py
case VarEnum.VT_I1:
case VarEnum.VT_I2:
case VarEnum.VT_I4:
case VarEnum.VT_I8:
case VarEnum.VT_UI1:
case VarEnum.VT_UI2:
case VarEnum.VT_UI4:
case VarEnum.VT_UI8:
case VarEnum.VT_INT:
case VarEnum.VT_UINT:
case VarEnum.VT_BOOL:
case VarEnum.VT_ERROR:
case VarEnum.VT_R4:
case VarEnum.VT_R8:
case VarEnum.VT_DECIMAL:
case VarEnum.VT_CY:
case VarEnum.VT_DATE:
case VarEnum.VT_BSTR:
// *** END GENERATED CODE ***
#endregion
return true;
}
return false;
}
/// <summary>
/// Get the managed object representing the Variant.
/// </summary>
/// <returns></returns>
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
public object ToObject() {
// Check the simple case upfront
if (IsEmpty) {
return null;
}
switch (VariantType) {
case VarEnum.VT_NULL: return DBNull.Value;
#region Generated Outer Variant ToObject
// *** BEGIN GENERATED CODE ***
// generated by function: gen_ToObject from: generate_comdispatch.py
case VarEnum.VT_I1: return AsI1;
case VarEnum.VT_I2: return AsI2;
case VarEnum.VT_I4: return AsI4;
case VarEnum.VT_I8: return AsI8;
case VarEnum.VT_UI1: return AsUi1;
case VarEnum.VT_UI2: return AsUi2;
case VarEnum.VT_UI4: return AsUi4;
case VarEnum.VT_UI8: return AsUi8;
case VarEnum.VT_INT: return AsInt;
case VarEnum.VT_UINT: return AsUint;
case VarEnum.VT_BOOL: return AsBool;
case VarEnum.VT_ERROR: return AsError;
case VarEnum.VT_R4: return AsR4;
case VarEnum.VT_R8: return AsR8;
case VarEnum.VT_DECIMAL: return AsDecimal;
case VarEnum.VT_CY: return AsCy;
case VarEnum.VT_DATE: return AsDate;
case VarEnum.VT_BSTR: return AsBstr;
case VarEnum.VT_UNKNOWN: return AsUnknown;
case VarEnum.VT_DISPATCH: return AsDispatch;
case VarEnum.VT_VARIANT: return AsVariant;
// *** END GENERATED CODE ***
#endregion
default:
return AsVariant;
}
}
/// <summary>
/// Release any unmanaged memory associated with the Variant
/// </summary>
/// <returns></returns>
public void Clear() {
// We do not need to call OLE32's VariantClear for primitive types or ByRefs
// to safe ourselves the cost of interop transition.
// ByRef indicates the memory is not owned by the VARIANT itself while
// primitive types do not have any resources to free up.
// Hence, only safearrays, BSTRs, interfaces and user types are
// handled differently.
VarEnum vt = VariantType;
if ((vt & VarEnum.VT_BYREF) != 0) {
VariantType = VarEnum.VT_EMPTY;
} else if (
((vt & VarEnum.VT_ARRAY) != 0) ||
((vt) == VarEnum.VT_BSTR) ||
((vt) == VarEnum.VT_UNKNOWN) ||
((vt) == VarEnum.VT_DISPATCH) ||
((vt) == VarEnum.VT_RECORD)
) {
IntPtr variantPtr = UnsafeMethods.ConvertVariantByrefToPtr(ref this);
NativeMethods.VariantClear(variantPtr);
Debug.Assert(IsEmpty);
} else {
VariantType = VarEnum.VT_EMPTY;
}
}
public VarEnum VariantType {
get {
return (VarEnum)_typeUnion._vt;
}
set {
_typeUnion._vt = (ushort)value;
}
}
internal bool IsEmpty {
get {
return _typeUnion._vt == ((ushort)VarEnum.VT_EMPTY);
}
}
public void SetAsNull() {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_NULL;
}
public void SetAsIConvertible(IConvertible value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
TypeCode tc = value.GetTypeCode();
CultureInfo ci = CultureInfo.CurrentCulture;
switch (tc) {
case TypeCode.Empty: break;
case TypeCode.Object: AsUnknown = value; break;
case TypeCode.DBNull: SetAsNull(); break;
case TypeCode.Boolean: AsBool = value.ToBoolean(ci); break;
case TypeCode.Char: AsUi2 = value.ToChar(ci); break;
case TypeCode.SByte: AsI1 = value.ToSByte(ci); break;
case TypeCode.Byte: AsUi1 = value.ToByte(ci); break;
case TypeCode.Int16: AsI2 = value.ToInt16(ci); break;
case TypeCode.UInt16: AsUi2 = value.ToUInt16(ci); break;
case TypeCode.Int32: AsI4 = value.ToInt32(ci); break;
case TypeCode.UInt32: AsUi4 = value.ToUInt32(ci); break;
case TypeCode.Int64: AsI8 = value.ToInt64(ci); break;
case TypeCode.UInt64: AsI8 = value.ToInt64(ci); break;
case TypeCode.Single: AsR4 = value.ToSingle(ci); break;
case TypeCode.Double: AsR8 = value.ToDouble(ci); break;
case TypeCode.Decimal: AsDecimal = value.ToDecimal(ci); break;
case TypeCode.DateTime: AsDate = value.ToDateTime(ci); break;
case TypeCode.String: AsBstr = value.ToString(ci); break;
default:
throw Assert.Unreachable;
}
}
#region Generated Outer Variant accessors
// *** BEGIN GENERATED CODE ***
// generated by function: gen_accessors from: generate_comdispatch.py
// VT_I1
public SByte AsI1 {
get {
Debug.Assert(VariantType == VarEnum.VT_I1);
return _typeUnion._unionTypes._i1;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_I1;
_typeUnion._unionTypes._i1 = value;
}
}
public void SetAsByrefI1(ref SByte value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_I1 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertSByteByrefToPtr(ref value);
}
// VT_I2
public Int16 AsI2 {
get {
Debug.Assert(VariantType == VarEnum.VT_I2);
return _typeUnion._unionTypes._i2;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_I2;
_typeUnion._unionTypes._i2 = value;
}
}
public void SetAsByrefI2(ref Int16 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_I2 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertInt16ByrefToPtr(ref value);
}
// VT_I4
public Int32 AsI4 {
get {
Debug.Assert(VariantType == VarEnum.VT_I4);
return _typeUnion._unionTypes._i4;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_I4;
_typeUnion._unionTypes._i4 = value;
}
}
public void SetAsByrefI4(ref Int32 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_I4 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertInt32ByrefToPtr(ref value);
}
// VT_I8
public Int64 AsI8 {
get {
Debug.Assert(VariantType == VarEnum.VT_I8);
return _typeUnion._unionTypes._i8;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_I8;
_typeUnion._unionTypes._i8 = value;
}
}
public void SetAsByrefI8(ref Int64 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_I8 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertInt64ByrefToPtr(ref value);
}
// VT_UI1
public Byte AsUi1 {
get {
Debug.Assert(VariantType == VarEnum.VT_UI1);
return _typeUnion._unionTypes._ui1;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_UI1;
_typeUnion._unionTypes._ui1 = value;
}
}
public void SetAsByrefUi1(ref Byte value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_UI1 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertByteByrefToPtr(ref value);
}
// VT_UI2
public UInt16 AsUi2 {
get {
Debug.Assert(VariantType == VarEnum.VT_UI2);
return _typeUnion._unionTypes._ui2;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_UI2;
_typeUnion._unionTypes._ui2 = value;
}
}
public void SetAsByrefUi2(ref UInt16 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_UI2 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertUInt16ByrefToPtr(ref value);
}
// VT_UI4
public UInt32 AsUi4 {
get {
Debug.Assert(VariantType == VarEnum.VT_UI4);
return _typeUnion._unionTypes._ui4;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_UI4;
_typeUnion._unionTypes._ui4 = value;
}
}
public void SetAsByrefUi4(ref UInt32 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_UI4 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertUInt32ByrefToPtr(ref value);
}
// VT_UI8
public UInt64 AsUi8 {
get {
Debug.Assert(VariantType == VarEnum.VT_UI8);
return _typeUnion._unionTypes._ui8;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_UI8;
_typeUnion._unionTypes._ui8 = value;
}
}
public void SetAsByrefUi8(ref UInt64 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_UI8 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertUInt64ByrefToPtr(ref value);
}
// VT_INT
public IntPtr AsInt {
get {
Debug.Assert(VariantType == VarEnum.VT_INT);
return _typeUnion._unionTypes._int;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_INT;
_typeUnion._unionTypes._int = value;
}
}
public void SetAsByrefInt(ref IntPtr value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_INT | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertIntPtrByrefToPtr(ref value);
}
// VT_UINT
public UIntPtr AsUint {
get {
Debug.Assert(VariantType == VarEnum.VT_UINT);
return _typeUnion._unionTypes._uint;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_UINT;
_typeUnion._unionTypes._uint = value;
}
}
public void SetAsByrefUint(ref UIntPtr value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_UINT | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertUIntPtrByrefToPtr(ref value);
}
// VT_BOOL
public Boolean AsBool {
get {
Debug.Assert(VariantType == VarEnum.VT_BOOL);
return _typeUnion._unionTypes._bool != 0;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_BOOL;
_typeUnion._unionTypes._bool = value ? (Int16)(-1) : (Int16)0;
}
}
public void SetAsByrefBool(ref Int16 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_BOOL | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertInt16ByrefToPtr(ref value);
}
// VT_ERROR
public Int32 AsError {
get {
Debug.Assert(VariantType == VarEnum.VT_ERROR);
return _typeUnion._unionTypes._error;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_ERROR;
_typeUnion._unionTypes._error = value;
}
}
public void SetAsByrefError(ref Int32 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_ERROR | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertInt32ByrefToPtr(ref value);
}
// VT_R4
public Single AsR4 {
get {
Debug.Assert(VariantType == VarEnum.VT_R4);
return _typeUnion._unionTypes._r4;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_R4;
_typeUnion._unionTypes._r4 = value;
}
}
public void SetAsByrefR4(ref Single value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_R4 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertSingleByrefToPtr(ref value);
}
// VT_R8
public Double AsR8 {
get {
Debug.Assert(VariantType == VarEnum.VT_R8);
return _typeUnion._unionTypes._r8;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_R8;
_typeUnion._unionTypes._r8 = value;
}
}
public void SetAsByrefR8(ref Double value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_R8 | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertDoubleByrefToPtr(ref value);
}
// VT_DECIMAL
public Decimal AsDecimal {
get {
Debug.Assert(VariantType == VarEnum.VT_DECIMAL);
// The first byte of Decimal is unused, but usually set to 0
Variant v = this;
v._typeUnion._vt = 0;
return v._decimal;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_DECIMAL;
_decimal = value;
// _vt overlaps with _decimal, and should be set after setting _decimal
_typeUnion._vt = (ushort)VarEnum.VT_DECIMAL;
}
}
public void SetAsByrefDecimal(ref Decimal value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_DECIMAL | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertDecimalByrefToPtr(ref value);
}
// VT_CY
public Decimal AsCy {
get {
Debug.Assert(VariantType == VarEnum.VT_CY);
return Decimal.FromOACurrency(_typeUnion._unionTypes._cy);
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_CY;
_typeUnion._unionTypes._cy = Decimal.ToOACurrency(value);
}
}
public void SetAsByrefCy(ref Int64 value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_CY | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertInt64ByrefToPtr(ref value);
}
// VT_DATE
public DateTime AsDate {
get {
Debug.Assert(VariantType == VarEnum.VT_DATE);
return DateTime.FromOADate(_typeUnion._unionTypes._date);
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_DATE;
_typeUnion._unionTypes._date = value.ToOADate();
}
}
public void SetAsByrefDate(ref Double value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_DATE | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertDoubleByrefToPtr(ref value);
}
// VT_BSTR
public String AsBstr {
get {
Debug.Assert(VariantType == VarEnum.VT_BSTR);
if (_typeUnion._unionTypes._bstr != IntPtr.Zero) {
return Marshal.PtrToStringBSTR(_typeUnion._unionTypes._bstr);
}
return null;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_BSTR;
if (value != null) {
Marshal.GetNativeVariantForObject(value, UnsafeMethods.ConvertVariantByrefToPtr(ref this));
}
}
}
public void SetAsByrefBstr(ref IntPtr value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_BSTR | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertIntPtrByrefToPtr(ref value);
}
// VT_UNKNOWN
public Object AsUnknown {
get {
Debug.Assert(VariantType == VarEnum.VT_UNKNOWN);
if (_typeUnion._unionTypes._dispatch != IntPtr.Zero) {
return Marshal.GetObjectForIUnknown(_typeUnion._unionTypes._unknown);
}
return null;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_UNKNOWN;
if (value != null) {
_typeUnion._unionTypes._unknown = Marshal.GetIUnknownForObject(value);
}
}
}
public void SetAsByrefUnknown(ref IntPtr value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_UNKNOWN | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertIntPtrByrefToPtr(ref value);
}
// VT_DISPATCH
public Object AsDispatch {
get {
Debug.Assert(VariantType == VarEnum.VT_DISPATCH);
if (_typeUnion._unionTypes._dispatch != IntPtr.Zero) {
return Marshal.GetObjectForIUnknown(_typeUnion._unionTypes._dispatch);
}
return null;
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = VarEnum.VT_DISPATCH;
if (value != null) {
_typeUnion._unionTypes._unknown = GetIDispatchForObject(value);
}
}
}
public void SetAsByrefDispatch(ref IntPtr value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_DISPATCH | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertIntPtrByrefToPtr(ref value);
}
// *** END GENERATED CODE ***
#endregion
/// <summary>
/// Helper method for generated code
/// </summary>
private static IntPtr GetIDispatchForObject(object value) {
#if NETFRAMEWORK || NET
return Marshal.GetIDispatchForObject(value);
#else
return Marshal.GetComInterfaceForObject<object, IDispatch>(value);
#endif
}
// VT_VARIANT
public Object AsVariant {
get {
return Marshal.GetObjectForNativeVariant(UnsafeMethods.ConvertVariantByrefToPtr(ref this));
}
set {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
if (value != null) {
UnsafeMethods.InitVariantForObject(value, ref this);
}
}
}
public void SetAsByrefVariant(ref Variant value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
VariantType = (VarEnum.VT_VARIANT | VarEnum.VT_BYREF);
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertVariantByrefToPtr(ref value);
}
// constructs a ByRef variant to pass contents of another variant ByRef.
public void SetAsByrefVariantIndirect(ref Variant value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
Debug.Assert((value.VariantType & VarEnum.VT_BYREF) == 0, "double indirection");
switch (value.VariantType) {
case VarEnum.VT_EMPTY:
case VarEnum.VT_NULL:
// these cannot combine with VT_BYREF. Should try passing as a variant reference
SetAsByrefVariant(ref value);
return;
case VarEnum.VT_RECORD:
// VT_RECORD's are weird in that regardless of is the VT_BYREF flag is set or not
// they have the same internal representation.
_typeUnion._unionTypes._record = value._typeUnion._unionTypes._record;
break;
case VarEnum.VT_DECIMAL:
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertDecimalByrefToPtr(ref value._decimal);
break;
default:
_typeUnion._unionTypes._byref = UnsafeMethods.ConvertIntPtrByrefToPtr(ref value._typeUnion._unionTypes._byref);
break;
}
VariantType = (value.VariantType | VarEnum.VT_BYREF);
}
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
internal static System.Reflection.PropertyInfo GetAccessor(VarEnum varType) {
switch (varType) {
#region Generated Outer Variant accessors PropertyInfos
// *** BEGIN GENERATED CODE ***
// generated by function: gen_accessor_propertyinfo from: generate_comdispatch.py
case VarEnum.VT_I1: return typeof(Variant).GetProperty(nameof(Variant.AsI1));
case VarEnum.VT_I2: return typeof(Variant).GetProperty(nameof(Variant.AsI2));
case VarEnum.VT_I4: return typeof(Variant).GetProperty(nameof(Variant.AsI4));
case VarEnum.VT_I8: return typeof(Variant).GetProperty(nameof(Variant.AsI8));
case VarEnum.VT_UI1: return typeof(Variant).GetProperty(nameof(Variant.AsUi1));
case VarEnum.VT_UI2: return typeof(Variant).GetProperty(nameof(Variant.AsUi2));
case VarEnum.VT_UI4: return typeof(Variant).GetProperty(nameof(Variant.AsUi4));
case VarEnum.VT_UI8: return typeof(Variant).GetProperty(nameof(Variant.AsUi8));
case VarEnum.VT_INT: return typeof(Variant).GetProperty(nameof(Variant.AsInt));
case VarEnum.VT_UINT: return typeof(Variant).GetProperty(nameof(Variant.AsUint));
case VarEnum.VT_BOOL: return typeof(Variant).GetProperty(nameof(Variant.AsBool));
case VarEnum.VT_ERROR: return typeof(Variant).GetProperty(nameof(Variant.AsError));
case VarEnum.VT_R4: return typeof(Variant).GetProperty(nameof(Variant.AsR4));
case VarEnum.VT_R8: return typeof(Variant).GetProperty(nameof(Variant.AsR8));
case VarEnum.VT_DECIMAL: return typeof(Variant).GetProperty(nameof(Variant.AsDecimal));
case VarEnum.VT_CY: return typeof(Variant).GetProperty(nameof(Variant.AsCy));
case VarEnum.VT_DATE: return typeof(Variant).GetProperty(nameof(Variant.AsDate));
case VarEnum.VT_BSTR: return typeof(Variant).GetProperty(nameof(Variant.AsBstr));
case VarEnum.VT_UNKNOWN: return typeof(Variant).GetProperty(nameof(Variant.AsUnknown));
case VarEnum.VT_DISPATCH: return typeof(Variant).GetProperty(nameof(Variant.AsDispatch));
// *** END GENERATED CODE ***
#endregion
case VarEnum.VT_VARIANT:
case VarEnum.VT_RECORD:
case VarEnum.VT_ARRAY:
return typeof(Variant).GetProperty(nameof(Variant.AsVariant));
default:
throw Error.VariantGetAccessorNYI(varType);
}
}
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
internal static System.Reflection.MethodInfo GetByrefSetter(VarEnum varType) {
switch (varType) {
#region Generated Outer Variant byref setter
// *** BEGIN GENERATED CODE ***
// generated by function: gen_byref_setters from: generate_comdispatch.py
case VarEnum.VT_I1: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefI1));
case VarEnum.VT_I2: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefI2));
case VarEnum.VT_I4: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefI4));
case VarEnum.VT_I8: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefI8));
case VarEnum.VT_UI1: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefUi1));
case VarEnum.VT_UI2: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefUi2));
case VarEnum.VT_UI4: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefUi4));
case VarEnum.VT_UI8: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefUi8));
case VarEnum.VT_INT: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefInt));
case VarEnum.VT_UINT: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefUint));
case VarEnum.VT_BOOL: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefBool));
case VarEnum.VT_ERROR: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefError));
case VarEnum.VT_R4: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefR4));
case VarEnum.VT_R8: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefR8));
case VarEnum.VT_DECIMAL: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefDecimal));
case VarEnum.VT_CY: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefCy));
case VarEnum.VT_DATE: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefDate));
case VarEnum.VT_BSTR: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefBstr));
case VarEnum.VT_UNKNOWN: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefUnknown));
case VarEnum.VT_DISPATCH: return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefDispatch));
// *** END GENERATED CODE ***
#endregion
case VarEnum.VT_VARIANT:
return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefVariant));
case VarEnum.VT_RECORD:
case VarEnum.VT_ARRAY:
return typeof(Variant).GetMethod(nameof(Variant.SetAsByrefVariantIndirect));
default:
throw Error.VariantGetAccessorNYI(varType);
}
}
unsafe internal void CopyFromIndirect(object value) {
VarEnum vt = (VarEnum)(((int)VariantType) & ~((int)VarEnum.VT_BYREF));
if (value == null) {
if (vt == VarEnum.VT_DISPATCH || vt == VarEnum.VT_UNKNOWN || vt == VarEnum.VT_BSTR) {
*(IntPtr*)_typeUnion._unionTypes._byref = IntPtr.Zero;
}
return;
}
if ((vt & VarEnum.VT_ARRAY) != 0) {
Variant vArray;
Marshal.GetNativeVariantForObject(value, (IntPtr)(void*)&vArray);
*(IntPtr*)_typeUnion._unionTypes._byref = vArray._typeUnion._unionTypes._byref;
return;
}
switch (vt) {
case VarEnum.VT_I1:
*(sbyte*)_typeUnion._unionTypes._byref = (sbyte)value;
break;
case VarEnum.VT_UI1:
*(byte*)_typeUnion._unionTypes._byref = (byte)value;
break;
case VarEnum.VT_I2:
*(short*)_typeUnion._unionTypes._byref = (short)value;
break;
case VarEnum.VT_UI2:
*(ushort*)_typeUnion._unionTypes._byref = (ushort)value;
break;
case VarEnum.VT_BOOL:
// VARIANT_TRUE = -1
// VARIANT_FALSE = 0
*(short*)_typeUnion._unionTypes._byref = (bool)value ? (short)-1 : (short)0;
break;
case VarEnum.VT_I4:
case VarEnum.VT_INT:
*(int*)_typeUnion._unionTypes._byref = (int)value;
break;
case VarEnum.VT_UI4:
case VarEnum.VT_UINT:
*(uint*)_typeUnion._unionTypes._byref = (uint)value;
break;
case VarEnum.VT_ERROR:
#pragma warning disable CS0618 // Type or member is obsolete
*(int*)_typeUnion._unionTypes._byref = ((ErrorWrapper)value).ErrorCode;
#pragma warning restore CS0618 // Type or member is obsolete
break;
case VarEnum.VT_I8:
*(Int64*)_typeUnion._unionTypes._byref = (Int64)value;
break;
case VarEnum.VT_UI8:
*(UInt64*)_typeUnion._unionTypes._byref = (UInt64)value;
break;
case VarEnum.VT_R4:
*(float*)_typeUnion._unionTypes._byref = (float)value;
break;
case VarEnum.VT_R8:
*(double*)_typeUnion._unionTypes._byref = (double)value;
break;
case VarEnum.VT_DATE:
*(double*)_typeUnion._unionTypes._byref = ((DateTime)value).ToOADate();
break;
case VarEnum.VT_UNKNOWN:
*(IntPtr*)_typeUnion._unionTypes._byref = Marshal.GetIUnknownForObject(value);
break;
case VarEnum.VT_DISPATCH:
*(IntPtr*)_typeUnion._unionTypes._byref = GetIDispatchForObject(value);
break;
case VarEnum.VT_BSTR:
*(IntPtr*)_typeUnion._unionTypes._byref = Marshal.StringToBSTR((string)value);
break;
case VarEnum.VT_CY:
*(long*)_typeUnion._unionTypes._byref = decimal.ToOACurrency((decimal)value);
break;
case VarEnum.VT_DECIMAL:
*(decimal*)_typeUnion._unionTypes._byref = (decimal)value;
break;
case VarEnum.VT_VARIANT:
Marshal.GetNativeVariantForObject(value, _typeUnion._unionTypes._byref);
break;
default:
throw new ArgumentException();
}
}
public IntPtr AsByRefVariant {
get {
Debug.Assert(VariantType == (VarEnum.VT_BYREF | VarEnum.VT_VARIANT));
return _typeUnion._unionTypes._byref;
}
}
}
}
#endif