From 6ce476ad75987580d756eb13c68e7c32aa4e0e23 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Thu, 29 Oct 2020 08:56:28 -0700 Subject: [PATCH] fix issue with FromManaged returning an HSTRING reference, rather than allocating an HSTRING, causing crashes --- WinRT.Runtime/Projections/Type.cs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/WinRT.Runtime/Projections/Type.cs b/WinRT.Runtime/Projections/Type.cs index df123c8d8..26eee1901 100644 --- a/WinRT.Runtime/Projections/Type.cs +++ b/WinRT.Runtime/Projections/Type.cs @@ -29,8 +29,8 @@ internal void Dispose() } } - public static Marshaler CreateMarshaler(global::System.Type value) - { + private static (String Name, TypeKind Kind) ToAbi(global::System.Type value) + { TypeKind kind = TypeKind.Custom; if (value is object) @@ -47,12 +47,18 @@ public static Marshaler CreateMarshaler(global::System.Type value) { kind = TypeKind.Metadata; } - } + } + + return (kind == TypeKind.Custom ? value.AssemblyQualifiedName : TypeNameSupport.GetNameForType(value, TypeNameGenerationFlags.None), kind); + } + public static Marshaler CreateMarshaler(global::System.Type value) + { + var abi = ToAbi(value); return new Marshaler { - Name = MarshalString.CreateMarshaler(kind == TypeKind.Custom ? value.AssemblyQualifiedName : TypeNameSupport.GetNameForType(value, TypeNameGenerationFlags.None)), - Kind = kind + Name = MarshalString.CreateMarshaler(abi.Name), + Kind = abi.Kind }; } @@ -85,8 +91,13 @@ public static unsafe void CopyAbi(Marshaler arg, IntPtr dest) => *(Type*)dest.ToPointer() = GetAbi(arg); public static Type FromManaged(global::System.Type value) - { - return GetAbi(CreateMarshaler(value)); + { + var abi = ToAbi(value); + return new Type + { + Name = MarshalString.FromManaged(abi.Name), + Kind = abi.Kind + }; } public static unsafe void CopyManaged(global::System.Type arg, IntPtr dest) =>