1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 /*============================================================
9 ** RuntimeClass is the base class of all WinRT types
12 ===========================================================*/
15 using System.Runtime.CompilerServices;
17 namespace System.Runtime.InteropServices.WindowsRuntime
19 // Local definition of Windows.Foundation.IStringable
21 [Guid("96369f54-8eb6-48f0-abce-c1b211e627c3")]
22 [WindowsRuntimeImport]
23 internal interface IStringable
28 internal class IStringableHelper
30 internal static string? ToString(object obj)
32 if (obj is IGetProxyTarget proxy)
33 obj = proxy.GetTarget();
35 // Check whether the type implements IStringable.
36 if (obj is IStringable stringableType)
38 return stringableType.ToString();
41 return obj.ToString();
46 // Base class for every WinRT class
47 // We'll make it a ComImport and WindowsRuntimeImport in the type loader
48 // as C# compiler won't allow putting code in ComImport type
50 internal abstract class RuntimeClass : __ComObject
53 // Support for ToString/GetHashCode/Equals override
55 [MethodImpl(MethodImplOptions.InternalCall)]
56 internal extern IntPtr GetRedirectedGetHashCodeMD();
58 [MethodImpl(MethodImplOptions.InternalCall)]
59 internal extern int RedirectGetHashCode(IntPtr pMD);
61 public override int GetHashCode()
63 IntPtr pMD = GetRedirectedGetHashCodeMD();
64 if (pMD == IntPtr.Zero)
65 return base.GetHashCode();
66 return RedirectGetHashCode(pMD);
69 [MethodImpl(MethodImplOptions.InternalCall)]
70 internal extern IntPtr GetRedirectedToStringMD();
72 [MethodImpl(MethodImplOptions.InternalCall)]
73 internal extern string RedirectToString(IntPtr pMD);
75 public override string ToString()
77 // Check whether the type implements IStringable.
78 if (this is IStringable stringableType)
80 return stringableType.ToString();
84 IntPtr pMD = GetRedirectedToStringMD();
86 if (pMD == IntPtr.Zero)
87 return base.ToString();
89 return RedirectToString(pMD);
93 [MethodImpl(MethodImplOptions.InternalCall)]
94 internal extern IntPtr GetRedirectedEqualsMD();
96 [MethodImpl(MethodImplOptions.InternalCall)]
97 internal extern bool RedirectEquals(object? obj, IntPtr pMD);
99 public override bool Equals(object? obj)
101 IntPtr pMD = GetRedirectedEqualsMD();
102 if (pMD == IntPtr.Zero)
103 return base.Equals(obj);
104 return RedirectEquals(obj, pMD);