summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/RuntimeClass.cs
blob: fdc0d2263257fbb9d0934420e7c45a7fd5bf7c73 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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.

/*============================================================
**
**
**
** RuntimeClass is the base class of all WinRT types
**
** 
===========================================================*/

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.CompilerServices;
using System.Security;

namespace System.Runtime.InteropServices.WindowsRuntime
{
    // Local definition of Windows.Foundation.IStringable
    [ComImport]
    [Guid("96369f54-8eb6-48f0-abce-c1b211e627c3")]
    [WindowsRuntimeImport]
    internal interface IStringable
    {
        string ToString();
    }

    internal class IStringableHelper
    {
        internal static string ToString(object obj)
        {
            IGetProxyTarget proxy = obj as IGetProxyTarget;
            if (proxy != null)
                obj = proxy.GetTarget();

            // Check whether the type implements IStringable.
            IStringable stringableType = obj as IStringable;
            if (stringableType != null)
            {
                return stringableType.ToString();
            }

            return obj.ToString();
        }
    }

    //
    // Base class for every WinRT class
    // We'll make it a ComImport and WindowsRuntimeImport in the type loader
    // as C# compiler won't allow putting code in ComImport type
    //
    internal abstract class RuntimeClass : __ComObject
    {
        //
        // Support for ToString/GetHashCode/Equals override
        //        
        [MethodImpl(MethodImplOptions.InternalCall)]
        internal extern IntPtr GetRedirectedGetHashCodeMD();

        [MethodImpl(MethodImplOptions.InternalCall)]
        internal extern int RedirectGetHashCode(IntPtr pMD);

        public override int GetHashCode()
        {
            IntPtr pMD = GetRedirectedGetHashCodeMD();
            if (pMD == IntPtr.Zero)
                return base.GetHashCode();
            return RedirectGetHashCode(pMD);
        }

        [MethodImpl(MethodImplOptions.InternalCall)]
        internal extern IntPtr GetRedirectedToStringMD();

        [MethodImpl(MethodImplOptions.InternalCall)]
        internal extern string RedirectToString(IntPtr pMD);

        public override string ToString()
        {
            // Check whether the type implements IStringable.
            IStringable stringableType = this as IStringable;
            if (stringableType != null)
            {
                return stringableType.ToString();
            }
            else
            {
                IntPtr pMD = GetRedirectedToStringMD();

                if (pMD == IntPtr.Zero)
                    return base.ToString();

                return RedirectToString(pMD);
            }
        }

        [MethodImpl(MethodImplOptions.InternalCall)]
        internal extern IntPtr GetRedirectedEqualsMD();

        [MethodImpl(MethodImplOptions.InternalCall)]
        internal extern bool RedirectEquals(object obj, IntPtr pMD);

        public override bool Equals(object obj)
        {
            IntPtr pMD = GetRedirectedEqualsMD();
            if (pMD == IntPtr.Zero)
                return base.Equals(obj);
            return RedirectEquals(obj, pMD);
        }
    }
}