/
Property.cs
815 lines (727 loc) · 35.4 KB
/
Property.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Management
{
// We use this class to prevent the accidental returning of a boxed value type to a caller
// If we store a boxed value type in a private field, and return it to the caller through a public
// property or method, the call can potentially change its value. The GetSafeObject method does two things
// 1) If the value is a primitive, we know that it will implement IConvertible. IConvertible.ToType will
// copy a boxed primitive
// 2) In the case of a boxed non-primitive value type, or simply a reference type, we call
// RuntimeHelpers.GetObjectValue. This returns reference types right back to the caller, but if passed
// a boxed non-primitive value type, it will return a boxed copy. We cannot use GetObjectValue for primitives
// because its implementation does not copy boxed primitives.
internal static class ValueTypeSafety
{
public static object GetSafeObject(object theValue)
{
if (null == theValue)
return null;
else if (theValue.GetType().IsPrimitive)
return ((IConvertible)theValue).ToType(typeof(object), null);
else
return RuntimeHelpers.GetObjectValue(theValue);
}
}
//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
/// <summary>
/// <para> Represents information about a WMI property.</para>
/// </summary>
/// <example>
/// <code lang='C#'>using System;
/// using System.Management;
///
/// // This sample displays all properties that qualifies the "DeviceID" property
/// // in Win32_LogicalDisk.DeviceID='C' instance.
/// class Sample_PropertyData
/// {
/// public static int Main(string[] args) {
/// ManagementObject disk =
/// new ManagementObject("Win32_LogicalDisk.DeviceID=\"C:\"");
/// PropertyData diskProperty = disk.Properties["DeviceID"];
/// Console.WriteLine("Name: " + diskProperty.Name);
/// Console.WriteLine("Type: " + diskProperty.Type);
/// Console.WriteLine("Value: " + diskProperty.Value);
/// Console.WriteLine("IsArray: " + diskProperty.IsArray);
/// Console.WriteLine("IsLocal: " + diskProperty.IsLocal);
/// Console.WriteLine("Origin: " + diskProperty.Origin);
/// return 0;
/// }
/// }
/// </code>
/// <code lang='VB'>Imports System
/// Imports System.Management
///
/// ' This sample displays all properties that qualifies the "DeviceID" property
/// ' in Win32_LogicalDisk.DeviceID='C' instance.
/// Class Sample_PropertyData
/// Overloads Public Shared Function Main(args() As String) As Integer
/// Dim disk As New ManagementObject("Win32_LogicalDisk.DeviceID=""C:""")
/// Dim diskProperty As PropertyData = disk.Properties("DeviceID")
/// Console.WriteLine("Name: " & diskProperty.Name)
/// Console.WriteLine("Type: " & diskProperty.Type)
/// Console.WriteLine("Value: " & diskProperty.Value)
/// Console.WriteLine("IsArray: " & diskProperty.IsArray)
/// Console.WriteLine("IsLocal: " & diskProperty.IsLocal)
/// Console.WriteLine("Origin: " & diskProperty.Origin)
/// Return 0
/// End Function
/// End Class
/// </code>
/// </example>
//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
public class PropertyData
{
private readonly ManagementBaseObject parent; //need access to IWbemClassObject pointer to be able to refresh property info
//and get property qualifiers
private readonly string propertyName;
private object propertyValue;
private long propertyNullEnumValue;
private int propertyType;
private int propertyFlavor;
private QualifierDataCollection qualifiers;
internal PropertyData(ManagementBaseObject parent, string propName)
{
this.parent = parent;
this.propertyName = propName;
qualifiers = null;
RefreshPropertyInfo();
}
//This private function is used to refresh the information from the Wmi object before returning the requested data
private void RefreshPropertyInfo()
{
propertyValue = null; // Needed so we don't leak this in/out parameter...
int status = parent.wbemObject.Get_(propertyName, 0, ref propertyValue, ref propertyType, ref propertyFlavor);
if (status < 0)
{
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else
Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
}
}
/// <summary>
/// <para>Gets or sets the name of the property.</para>
/// </summary>
/// <value>
/// A string containing the name of the
/// property.
/// </value>
public string Name
{ //doesn't change for this object so we don't need to refresh
get { return propertyName ?? ""; }
}
/// <summary>
/// <para>Gets or sets the current value of the property.</para>
/// </summary>
/// <value>
/// An object containing the value of the
/// property.
/// </value>
public object Value
{
get
{
RefreshPropertyInfo();
return ValueTypeSafety.GetSafeObject(MapWmiValueToValue(propertyValue,
(CimType)(propertyType & ~(int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY),
(0 != (propertyType & (int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY))));
}
set
{
RefreshPropertyInfo();
object newValue = MapValueToWmiValue(value,
(CimType)(propertyType & ~(int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY),
(0 != (propertyType & (int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY)));
int status = parent.wbemObject.Put_(propertyName, 0, ref newValue, 0);
if (status < 0)
{
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else
Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
}
//if succeeded and this object has a path, update the path to reflect the new key value
//NOTE : we could only do this for key properties but since it's not trivial to find out
// whether this property is a key or not, we just do it for any property
else
if (parent.GetType() == typeof(ManagementObject))
((ManagementObject)parent).Path.UpdateRelativePath((string)parent["__RELPATH"]);
}
}
/// <summary>
/// <para>Gets or sets the CIM type of the property.</para>
/// </summary>
/// <value>
/// <para>A <see cref='System.Management.CimType'/> value
/// representing the CIM type of the property.</para>
/// </value>
public CimType Type
{
get
{
RefreshPropertyInfo();
return (CimType)(propertyType & ~(int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY);
}
}
/// <summary>
/// <para>Gets or sets a value indicating whether the property has been defined in the current WMI class.</para>
/// </summary>
/// <value>
/// <para><see langword='true'/> if the property has been defined
/// in the current WMI class; otherwise, <see langword='false'/>.</para>
/// </value>
public bool IsLocal
{
get
{
RefreshPropertyInfo();
return ((propertyFlavor & (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_ORIGIN_PROPAGATED) != 0) ? false : true;
}
}
/// <summary>
/// <para>Gets or sets a value indicating whether the property is an array.</para>
/// </summary>
/// <value>
/// <para><see langword='true'/> if the property is an array; otherwise, <see langword='false'/>.</para>
/// </value>
public bool IsArray
{
get
{
RefreshPropertyInfo();
return ((propertyType & (int)tag_CIMTYPE_ENUMERATION.CIM_FLAG_ARRAY) != 0);
}
}
/// <summary>
/// <para>Gets or sets the name of the WMI class in the hierarchy in which the property was introduced.</para>
/// </summary>
/// <value>
/// A string containing the name of the
/// originating WMI class.
/// </value>
public string Origin
{
get
{
string className = null;
int status = parent.wbemObject.GetPropertyOrigin_(propertyName, out className);
if (status < 0)
{
if (status == (int)tag_WBEMSTATUS.WBEM_E_INVALID_OBJECT)
className = string.Empty; // Interpret as an unspecified property - return ""
else if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else
Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
}
return className;
}
}
/// <summary>
/// <para>Gets or sets the set of qualifiers defined on the property.</para>
/// </summary>
/// <value>
/// <para>A <see cref='System.Management.QualifierDataCollection'/> that represents
/// the set of qualifiers defined on the property.</para>
/// </value>
public QualifierDataCollection Qualifiers => qualifiers ??= new QualifierDataCollection(parent, propertyName, QualifierType.PropertyQualifier);
internal long NullEnumValue
{
get
{
return propertyNullEnumValue;
}
set
{
propertyNullEnumValue = value;
}
}
/// <summary>
/// Takes a property value returned from WMI and maps it to an
/// appropriate managed code representation.
/// </summary>
/// <param name="wmiValue"> </param>
/// <param name="type"> </param>
/// <param name="isArray"> </param>
internal static object MapWmiValueToValue(object wmiValue, CimType type, bool isArray)
{
object val = null;
if ((System.DBNull.Value != wmiValue) && (null != wmiValue))
{
if (isArray)
{
Array wmiValueArray = (Array)wmiValue;
int length = wmiValueArray.Length;
switch (type)
{
case CimType.UInt16:
val = new ushort[length];
for (int i = 0; i < length; i++)
((ushort[])val)[i] = (ushort)((int)(wmiValueArray.GetValue(i)));
break;
case CimType.UInt32:
val = new uint[length];
for (int i = 0; i < length; i++)
((uint[])val)[i] = (uint)((int)(wmiValueArray.GetValue(i)));
break;
case CimType.UInt64:
val = new ulong[length];
for (int i = 0; i < length; i++)
((ulong[])val)[i] = Convert.ToUInt64((string)(wmiValueArray.GetValue(i)), (IFormatProvider)CultureInfo.CurrentCulture.GetFormat(typeof(ulong)));
break;
case CimType.SInt8:
val = new sbyte[length];
for (int i = 0; i < length; i++)
((sbyte[])val)[i] = (sbyte)((short)(wmiValueArray.GetValue(i)));
break;
case CimType.SInt64:
val = new long[length];
for (int i = 0; i < length; i++)
((long[])val)[i] = Convert.ToInt64((string)(wmiValueArray.GetValue(i)), (IFormatProvider)CultureInfo.CurrentCulture.GetFormat(typeof(long)));
break;
case CimType.Char16:
val = new char[length];
for (int i = 0; i < length; i++)
((char[])val)[i] = (char)((short)(wmiValueArray.GetValue(i)));
break;
case CimType.Object:
val = new ManagementBaseObject[length];
for (int i = 0; i < length; i++)
((ManagementBaseObject[])val)[i] = new ManagementBaseObject(new IWbemClassObjectFreeThreaded(Marshal.GetIUnknownForObject(wmiValueArray.GetValue(i))));
break;
default:
val = wmiValue;
break;
}
}
else
{
val = type switch
{
CimType.SInt8 => (sbyte)((short)wmiValue),
CimType.UInt16 => (ushort)((int)wmiValue),
CimType.UInt32 => (uint)((int)wmiValue),
CimType.UInt64 => Convert.ToUInt64((string)wmiValue, (IFormatProvider)CultureInfo.CurrentCulture.GetFormat(typeof(ulong))),
CimType.SInt64 => Convert.ToInt64((string)wmiValue, (IFormatProvider)CultureInfo.CurrentCulture.GetFormat(typeof(long))),
CimType.Char16 => (char)((short)wmiValue),
CimType.Object => new ManagementBaseObject(new IWbemClassObjectFreeThreaded(Marshal.GetIUnknownForObject(wmiValue))),
_ => wmiValue,
};
}
}
return val;
}
/// <summary>
/// Takes a managed code value, together with a desired property
/// </summary>
/// <param name="val"> </param>
/// <param name="type"> </param>
/// <param name="isArray"> </param>
internal static object MapValueToWmiValue(object val, CimType type, bool isArray)
{
object wmiValue = System.DBNull.Value;
CultureInfo culInfo = CultureInfo.InvariantCulture;
if (null != val)
{
if (isArray)
{
Array valArray = (Array)val;
int length = valArray.Length;
switch (type)
{
case CimType.SInt8:
wmiValue = new short[length];
for (int i = 0; i < length; i++)
((short[])(wmiValue))[i] = (short)Convert.ToSByte(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(sbyte)));
break;
case CimType.UInt8:
if (val is byte[])
wmiValue = val;
else
{
wmiValue = new byte[length];
for (int i = 0; i < length; i++)
((byte[])wmiValue)[i] = Convert.ToByte(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(byte)));
}
break;
case CimType.SInt16:
if (val is short[])
wmiValue = val;
else
{
wmiValue = new short[length];
for (int i = 0; i < length; i++)
((short[])(wmiValue))[i] = Convert.ToInt16(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(short)));
}
break;
case CimType.UInt16:
wmiValue = new int[length];
for (int i = 0; i < length; i++)
((int[])(wmiValue))[i] = (int)(Convert.ToUInt16(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(ushort))));
break;
case CimType.SInt32:
if (val is int[])
wmiValue = val;
else
{
wmiValue = new int[length];
for (int i = 0; i < length; i++)
((int[])(wmiValue))[i] = Convert.ToInt32(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(int)));
}
break;
case CimType.UInt32:
wmiValue = new int[length];
for (int i = 0; i < length; i++)
((int[])(wmiValue))[i] = (int)(Convert.ToUInt32(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(uint))));
break;
case CimType.SInt64:
wmiValue = new string[length];
for (int i = 0; i < length; i++)
((string[])(wmiValue))[i] = (Convert.ToInt64(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(long)))).ToString((IFormatProvider)culInfo.GetFormat(typeof(long)));
break;
case CimType.UInt64:
wmiValue = new string[length];
for (int i = 0; i < length; i++)
((string[])(wmiValue))[i] = (Convert.ToUInt64(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(ulong)))).ToString((IFormatProvider)culInfo.GetFormat(typeof(ulong)));
break;
case CimType.Real32:
if (val is float[])
wmiValue = val;
else
{
wmiValue = new float[length];
for (int i = 0; i < length; i++)
((float[])(wmiValue))[i] = Convert.ToSingle(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(float)));
}
break;
case CimType.Real64:
if (val is double[])
wmiValue = val;
else
{
wmiValue = new double[length];
for (int i = 0; i < length; i++)
((double[])(wmiValue))[i] = Convert.ToDouble(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(double)));
}
break;
case CimType.Char16:
wmiValue = new short[length];
for (int i = 0; i < length; i++)
((short[])(wmiValue))[i] = (short)Convert.ToChar(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(char)));
break;
case CimType.String:
case CimType.DateTime:
case CimType.Reference:
if (val is string[])
wmiValue = val;
else
{
wmiValue = new string[length];
for (int i = 0; i < length; i++)
((string[])(wmiValue))[i] = (valArray.GetValue(i)).ToString();
}
break;
case CimType.Boolean:
if (val is bool[])
wmiValue = val;
else
{
wmiValue = new bool[length];
for (int i = 0; i < length; i++)
((bool[])(wmiValue))[i] = Convert.ToBoolean(valArray.GetValue(i), (IFormatProvider)culInfo.GetFormat(typeof(bool)));
}
break;
case CimType.Object:
wmiValue = new IWbemClassObject_DoNotMarshal[length];
for (int i = 0; i < length; i++)
{
((IWbemClassObject_DoNotMarshal[])(wmiValue))[i] = (IWbemClassObject_DoNotMarshal)(Marshal.GetObjectForIUnknown(((ManagementBaseObject)valArray.GetValue(i)).wbemObject));
}
break;
default:
wmiValue = val;
break;
}
}
else
{
switch (type)
{
case CimType.SInt8:
wmiValue = (short)Convert.ToSByte(val, (IFormatProvider)culInfo.GetFormat(typeof(short)));
break;
case CimType.UInt8:
wmiValue = Convert.ToByte(val, (IFormatProvider)culInfo.GetFormat(typeof(byte)));
break;
case CimType.SInt16:
wmiValue = Convert.ToInt16(val, (IFormatProvider)culInfo.GetFormat(typeof(short)));
break;
case CimType.UInt16:
wmiValue = (int)(Convert.ToUInt16(val, (IFormatProvider)culInfo.GetFormat(typeof(ushort))));
break;
case CimType.SInt32:
wmiValue = Convert.ToInt32(val, (IFormatProvider)culInfo.GetFormat(typeof(int)));
break;
case CimType.UInt32:
wmiValue = (int)Convert.ToUInt32(val, (IFormatProvider)culInfo.GetFormat(typeof(uint)));
break;
case CimType.SInt64:
wmiValue = (Convert.ToInt64(val, (IFormatProvider)culInfo.GetFormat(typeof(long)))).ToString((IFormatProvider)culInfo.GetFormat(typeof(long)));
break;
case CimType.UInt64:
wmiValue = (Convert.ToUInt64(val, (IFormatProvider)culInfo.GetFormat(typeof(ulong)))).ToString((IFormatProvider)culInfo.GetFormat(typeof(ulong)));
break;
case CimType.Real32:
wmiValue = Convert.ToSingle(val, (IFormatProvider)culInfo.GetFormat(typeof(float)));
break;
case CimType.Real64:
wmiValue = Convert.ToDouble(val, (IFormatProvider)culInfo.GetFormat(typeof(double)));
break;
case CimType.Char16:
wmiValue = (short)Convert.ToChar(val, (IFormatProvider)culInfo.GetFormat(typeof(char)));
break;
case CimType.String:
case CimType.DateTime:
case CimType.Reference:
wmiValue = val.ToString();
break;
case CimType.Boolean:
wmiValue = Convert.ToBoolean(val, (IFormatProvider)culInfo.GetFormat(typeof(bool)));
break;
case CimType.Object:
if (val is ManagementBaseObject)
{
wmiValue = Marshal.GetObjectForIUnknown(((ManagementBaseObject)val).wbemObject);
}
else
{
wmiValue = val;
}
break;
default:
wmiValue = val;
break;
}
}
}
return wmiValue;
}
internal static object MapValueToWmiValue(object val, out bool isArray, out CimType type)
{
object wmiValue = System.DBNull.Value;
CultureInfo culInfo = CultureInfo.InvariantCulture;
isArray = false;
type = 0;
if (null != val)
{
isArray = val.GetType().IsArray;
Type valueType = val.GetType();
if (isArray)
{
Type elementType = valueType.GetElementType();
// Casting primitive types to object[] is not allowed
if (elementType.IsPrimitive)
{
if (elementType == typeof(byte))
{
byte[] arrayValue = (byte[])val;
int length = arrayValue.Length;
type = CimType.UInt8;
wmiValue = new short[length];
for (int i = 0; i < length; i++)
((short[])wmiValue)[i] = ((IConvertible)((byte)(arrayValue[i]))).ToInt16(null);
}
else if (elementType == typeof(sbyte))
{
sbyte[] arrayValue = (sbyte[])val;
int length = arrayValue.Length;
type = CimType.SInt8;
wmiValue = new short[length];
for (int i = 0; i < length; i++)
((short[])wmiValue)[i] = ((IConvertible)((sbyte)(arrayValue[i]))).ToInt16(null);
}
else if (elementType == typeof(bool))
{
type = CimType.Boolean;
wmiValue = (bool[])val;
}
else if (elementType == typeof(ushort))
{
ushort[] arrayValue = (ushort[])val;
int length = arrayValue.Length;
type = CimType.UInt16;
wmiValue = new int[length];
for (int i = 0; i < length; i++)
((int[])wmiValue)[i] = ((IConvertible)((ushort)(arrayValue[i]))).ToInt32(null);
}
else if (elementType == typeof(short))
{
type = CimType.SInt16;
wmiValue = (short[])val;
}
else if (elementType == typeof(int))
{
type = CimType.SInt32;
wmiValue = (int[])val;
}
else if (elementType == typeof(uint))
{
uint[] arrayValue = (uint[])val;
int length = arrayValue.Length;
type = CimType.UInt32;
wmiValue = new string[length];
for (int i = 0; i < length; i++)
((string[])wmiValue)[i] = ((uint)(arrayValue[i])).ToString((IFormatProvider)culInfo.GetFormat(typeof(uint)));
}
else if (elementType == typeof(ulong))
{
ulong[] arrayValue = (ulong[])val;
int length = arrayValue.Length;
type = CimType.UInt64;
wmiValue = new string[length];
for (int i = 0; i < length; i++)
((string[])wmiValue)[i] = ((ulong)(arrayValue[i])).ToString((IFormatProvider)culInfo.GetFormat(typeof(ulong)));
}
else if (elementType == typeof(long))
{
long[] arrayValue = (long[])val;
int length = arrayValue.Length;
type = CimType.SInt64;
wmiValue = new string[length];
for (int i = 0; i < length; i++)
((string[])wmiValue)[i] = ((long)(arrayValue[i])).ToString((IFormatProvider)culInfo.GetFormat(typeof(long)));
}
else if (elementType == typeof(float))
{
type = CimType.Real32;
wmiValue = (float[])val;
}
else if (elementType == typeof(double))
{
type = CimType.Real64;
wmiValue = (double[])val;
}
else if (elementType == typeof(char))
{
char[] arrayValue = (char[])val;
int length = arrayValue.Length;
type = CimType.Char16;
wmiValue = new short[length];
for (int i = 0; i < length; i++)
((short[])wmiValue)[i] = ((IConvertible)((char)(arrayValue[i]))).ToInt16(null);
}
}
else
{
// Non-primitive types
if (elementType == typeof(string))
{
type = CimType.String;
wmiValue = (string[])val;
}
else
{
// Check for an embedded object array
if (val is ManagementBaseObject[])
{
Array valArray = (Array)val;
int length = valArray.Length;
type = CimType.Object;
wmiValue = new IWbemClassObject_DoNotMarshal[length];
for (int i = 0; i < length; i++)
{
((IWbemClassObject_DoNotMarshal[])(wmiValue))[i] = (IWbemClassObject_DoNotMarshal)(Marshal.GetObjectForIUnknown(((ManagementBaseObject)valArray.GetValue(i)).wbemObject));
}
}
}
}
}
else // Non-array values
{
if (valueType == typeof(ushort))
{
type = CimType.UInt16;
wmiValue = ((IConvertible)((ushort)val)).ToInt32(null);
}
else if (valueType == typeof(uint))
{
type = CimType.UInt32;
if (((uint)val & 0x80000000) != 0)
wmiValue = Convert.ToString(val, (IFormatProvider)culInfo.GetFormat(typeof(uint)));
else
wmiValue = Convert.ToInt32(val, (IFormatProvider)culInfo.GetFormat(typeof(int)));
}
else if (valueType == typeof(ulong))
{
type = CimType.UInt64;
wmiValue = ((ulong)val).ToString((IFormatProvider)culInfo.GetFormat(typeof(ulong)));
}
else if (valueType == typeof(sbyte))
{
type = CimType.SInt8;
wmiValue = ((IConvertible)((sbyte)val)).ToInt16(null);
}
else if (valueType == typeof(byte))
{
type = CimType.UInt8;
wmiValue = val;
}
else if (valueType == typeof(short))
{
type = CimType.SInt16;
wmiValue = val;
}
else if (valueType == typeof(int))
{
type = CimType.SInt32;
wmiValue = val;
}
else if (valueType == typeof(long))
{
type = CimType.SInt64;
wmiValue = val.ToString();
}
else if (valueType == typeof(bool))
{
type = CimType.Boolean;
wmiValue = val;
}
else if (valueType == typeof(float))
{
type = CimType.Real32;
wmiValue = val;
}
else if (valueType == typeof(double))
{
type = CimType.Real64;
wmiValue = val;
}
else if (valueType == typeof(char))
{
type = CimType.Char16;
wmiValue = ((IConvertible)((char)val)).ToInt16(null);
}
else if (valueType == typeof(string))
{
type = CimType.String;
wmiValue = val;
}
else
{
// Check for an embedded object
if (val is ManagementBaseObject)
{
type = CimType.Object;
wmiValue = Marshal.GetObjectForIUnknown(((ManagementBaseObject)val).wbemObject);
}
}
}
}
return wmiValue;
}
}//PropertyData
}