summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/OleAutBinder.cs
blob: 1db61bb30ef14e597106c5798c66656b0b033c99 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// This class represents the Ole Automation binder.

// #define DISPLAY_DEBUG_INFO

namespace System
{
    using System;
    using System.Runtime.InteropServices;
    using System.Reflection;
    using Microsoft.Win32;
    using CultureInfo = System.Globalization.CultureInfo;

    // Made serializable in anticipation of this class eventually having state.
    [Serializable]
    internal class OleAutBinder : DefaultBinder
    {
        // ChangeType
        // This binder uses OLEAUT to change the type of the variant.
        public override Object ChangeType(Object value, Type type, CultureInfo cultureInfo)
        {
            Variant myValue = new Variant(value);
            if (cultureInfo == null)
                cultureInfo = CultureInfo.CurrentCulture;

#if DISPLAY_DEBUG_INFO
            Console.WriteLine("In OleAutBinder::ChangeType converting variant of type: {0} to type: {1}", myValue.VariantType, type.Name);
#endif

            if (type.IsByRef)
            {
#if DISPLAY_DEBUG_INFO
                Console.WriteLine("Stripping byref from the type to convert to.");
#endif
                type = type.GetElementType();
            }

            // If we are trying to convert from an object to another type then we don't
            // need the OLEAUT change type, we can just use the normal COM+ mechanisms.
            if (!type.IsPrimitive && type.IsInstanceOfType(value))
            {
#if DISPLAY_DEBUG_INFO
                Console.WriteLine("Source variant can be assigned to destination type");
#endif
                return value;
            }

            Type srcType = value.GetType();

            // Handle converting primitives to enums.
            if (type.IsEnum && srcType.IsPrimitive)
            {
#if DISPLAY_DEBUG_INFO
                Console.WriteLine("Converting primitive to enum");
#endif
                return Enum.Parse(type, value.ToString());
            }

            // Use the OA variant lib to convert primitive types.
            try
            {
#if DISPLAY_DEBUG_INFO      
                Console.WriteLine("Using OAVariantLib.ChangeType() to do the conversion");
#endif      
                // Specify the LocalBool flag to have BOOL values converted to local language rather
                // than 0 or -1.
                Object RetObj = OAVariantLib.ChangeType(myValue, type, OAVariantLib.LocalBool, cultureInfo).ToObject();

#if DISPLAY_DEBUG_INFO      
                Console.WriteLine("Object returned from ChangeType is of type: " + RetObj.GetType().Name);
#endif

                return RetObj;
            }
#if DISPLAY_DEBUG_INFO      
            catch(NotSupportedException e)
#else
            catch (NotSupportedException)
#endif      
            {
#if DISPLAY_DEBUG_INFO      
                Console.Write("Exception thrown: ");
                Console.WriteLine(e);
#endif      
                throw new COMException(SR.Interop_COM_TypeMismatch, unchecked((int)0x80020005));
            }
        }
    }
}