summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/RuntimeClass.cs
blob: ee5602f61b00b13b41ce5d5f30babc8c0cd29c73 (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
114
115
116
117
118
119
120
121
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

/*============================================================
**
**
**
** RuntimeClass is the base class of all WinRT types
**
** 
===========================================================*/
namespace System.Runtime.InteropServices.WindowsRuntime {
    
    using System;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Runtime.CompilerServices;
    using System.Security;


    // 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
        //        
        [System.Security.SecurityCritical]
        [MethodImpl(MethodImplOptions.InternalCall)]            
        internal extern IntPtr GetRedirectedGetHashCodeMD();        
        
        [System.Security.SecurityCritical]
        [MethodImpl(MethodImplOptions.InternalCall)]            
        internal extern int RedirectGetHashCode(IntPtr pMD);        

        [System.Security.SecuritySafeCritical]
        public override int GetHashCode()
        {
            IntPtr pMD = GetRedirectedGetHashCodeMD();
            if (pMD == IntPtr.Zero)
                return base.GetHashCode();
            return RedirectGetHashCode(pMD);
        }

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

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

        [System.Security.SecuritySafeCritical]
        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);
            }
        }

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

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

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