From 52d0afc1c0e64957d1d1b8c00e90af6007a2f806 Mon Sep 17 00:00:00 2001 From: Jeremy Kuhne Date: Fri, 2 Feb 2024 19:08:25 -0800 Subject: [PATCH] Add Type info methods to VARIANT --- src/thirtytwo/Win32/System/Variant/VARIANT.cs | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/src/thirtytwo/Win32/System/Variant/VARIANT.cs b/src/thirtytwo/Win32/System/Variant/VARIANT.cs index 9e5d3e9..42217b6 100644 --- a/src/thirtytwo/Win32/System/Variant/VARIANT.cs +++ b/src/thirtytwo/Win32/System/Variant/VARIANT.cs @@ -121,4 +121,107 @@ public static explicit operator VARIANT(BSTR value) [MethodImpl(MethodImplOptions.NoInlining)] private static T* ThrowInvalidPointerCast() where T : unmanaged => throw new InvalidCastException(); + + /// + /// Returns the managed type returned from . + /// + public Type? GetManagedType() + { + if (IsEmpty) + { + return null; + } + + return GetManagedType(vt); + } + + /// + /// Returns the managed type returned from for the given . + /// + public static Type? GetManagedType(VARENUM type) + { + return type switch + { + VT_I1 => typeof(sbyte), + VT_UI1 => typeof(byte), + VT_I2 => typeof(short), + VT_UI2 => typeof(ushort), + VT_I4 => typeof(int), + VT_UI4 => typeof(uint), + VT_I8 => typeof(long), + VT_UI8 => typeof(ulong), + VT_INT => typeof(int), + VT_UINT => typeof(uint), + VT_R4 => typeof(float), + VT_R8 => typeof(double), + VT_BOOL => typeof(bool), + VT_ERROR => typeof(int), + VT_CY => typeof(decimal), + VT_DATE => typeof(DateTime), + VT_FILETIME => typeof(DateTime), + VT_BSTR => typeof(string), + VT_LPSTR => typeof(string), + VT_LPWSTR => typeof(string), + VT_DECIMAL => typeof(decimal), + VT_VARIANT => typeof(VARIANT), + VT_CLSID => typeof(Guid), + VT_BLOB => typeof(byte[]), + VT_STREAM => typeof(Stream), + VT_UNKNOWN => null, + VT_DISPATCH => null, + VT_CF => null, + VT_STREAMED_OBJECT => null, + VT_STORAGE => null, + VT_STORED_OBJECT => null, + VT_VERSIONED_STREAM => null, + _ => GetArrayType(type), + }; + + static Type? GetArrayType(VARENUM type) + { + if (!type.HasFlag(VT_ARRAY) && !type.HasFlag(VT_VECTOR)) + { + return null; + } + + type &= VT_TYPEMASK; + + return type switch + { + VT_I1 => typeof(sbyte[]), + VT_UI1 => typeof(byte[]), + VT_I2 => typeof(short[]), + VT_UI2 => typeof(ushort[]), + VT_I4 => typeof(int[]), + VT_UI4 => typeof(uint[]), + VT_I8 => typeof(long[]), + VT_UI8 => typeof(ulong[]), + VT_INT => typeof(int[]), + VT_UINT => typeof(uint[]), + VT_R4 => typeof(float[]), + VT_R8 => typeof(double[]), + VT_BOOL => typeof(bool[]), + VT_ERROR => typeof(int[]), + VT_CY => typeof(decimal[]), + VT_DATE => typeof(DateTime[]), + VT_FILETIME => typeof(DateTime[]), + VT_BSTR => typeof(string[]), + VT_LPSTR => typeof(string[]), + VT_LPWSTR => typeof(string[]), + VT_DECIMAL => typeof(decimal[]), + VT_VARIANT => typeof(VARIANT[]), + VT_CLSID => typeof(Guid[]), + VT_STREAM => typeof(Stream[]), + VT_BLOB => null, + VT_UNKNOWN => null, + VT_DISPATCH => null, + VT_CF => null, + VT_STREAMED_OBJECT => null, + VT_STORAGE => null, + VT_STORED_OBJECT => null, + VT_VERSIONED_STREAM => null, + _ => null, + }; + } + } } \ No newline at end of file