3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
11 ** Purpose: This class only exists to provide support for
12 ** implenting IDispatch on managed objects. It is
13 ** used to provide OleAut style coercion rules.
16 ===========================================================*/
17 namespace Microsoft.Win32 {
20 using System.Diagnostics.Contracts;
21 using System.Reflection;
22 using System.Runtime.CompilerServices;
23 using System.Runtime.Versioning;
24 using CultureInfo = System.Globalization.CultureInfo;
26 internal static class OAVariantLib
30 // Constants for VariantChangeType from OleAuto.h
31 public const int NoValueProp = 0x01;
32 public const int AlphaBool = 0x02;
33 public const int NoUserOverride = 0x04;
34 public const int CalendarHijri = 0x08;
35 public const int LocalBool = 0x10;
37 internal static readonly Type [] ClassTypes = {
58 null, // Enums - what do we do here?
63 // Keep these numbers in [....] w/ the above array.
64 private const int CV_OBJECT=0x12;
69 #region Internal Methods
72 * Changes a Variant from one type to another, calling the OLE
73 * Automation VariantChangeTypeEx routine. Note the legal types here are
74 * restricted to the subset of what can be legally found in a VB
75 * Variant and the types that CLR supports explicitly in the
78 [System.Security.SecurityCritical] // auto-generated
79 internal static Variant ChangeType(Variant source, Type targetClass, short options, CultureInfo culture)
81 if (targetClass == null)
82 throw new ArgumentNullException("targetClass");
84 throw new ArgumentNullException("culture");
85 Variant result = new Variant ();
86 ChangeTypeEx(ref result, ref source,
90 // @CORESYSTODO: what does CoreSystem expect for this argument?
93 targetClass.TypeHandle.Value, GetCVTypeFromClass(targetClass), options);
100 #region Private Helpers
102 private static int GetCVTypeFromClass(Type ctype)
104 Contract.Requires(ctype != null);
106 BCLDebug.Assert(ClassTypes[CV_OBJECT] == typeof(Object), "OAVariantLib::ClassTypes[CV_OBJECT] == Object.class");
110 for (int i=0; i<ClassTypes.Length; i++) {
111 if (ctype.Equals(ClassTypes[i])) {
117 // OleAut Binder works better if unrecognized
118 // types were changed to Object. So don't throw here.
128 #region Private FCalls
130 [System.Security.SecurityCritical] // auto-generated
131 [MethodImplAttribute(MethodImplOptions.InternalCall)]
132 [ResourceExposure(ResourceScope.None)]
133 private static extern void ChangeTypeEx(ref Variant result, ref Variant source, int lcid, IntPtr typeHandle, int cvType, short flags);