diff --git a/src/WinRT.Runtime/Projections/IDisposable.net5.cs b/src/WinRT.Runtime/Projections/IDisposable.net5.cs index baca42f8e..5785b36a8 100644 --- a/src/WinRT.Runtime/Projections/IDisposable.net5.cs +++ b/src/WinRT.Runtime/Projections/IDisposable.net5.cs @@ -21,7 +21,6 @@ unsafe interface IDisposable : global::System.IDisposable [Guid("30D5A829-7FA4-4026-83BB-D75BAE4EA99E")] public struct Vftbl { - private delegate int CloseDelegate(IntPtr thisPtr); internal IInspectable.Vftbl IInspectableVftbl; private void* _Close_0; public delegate* unmanaged[Stdcall] Close_0 { get => (delegate* unmanaged[Stdcall])_Close_0; set => _Close_0 = value; } @@ -35,9 +34,7 @@ static unsafe Vftbl() AbiToProjectionVftable = new Vftbl { IInspectableVftbl = global::WinRT.IInspectable.Vftbl.AbiToProjectionVftable, - _Close_0 = (delegate* unmanaged)&Do_Abi_Close_0 - }; var nativeVftbl = (IntPtr*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), Marshal.SizeOf() + sizeof(IntPtr) * 1); Marshal.StructureToPtr(AbiToProjectionVftable, (IntPtr)nativeVftbl, false); @@ -46,11 +43,8 @@ static unsafe Vftbl() [UnmanagedCallersOnly] - private static unsafe int Do_Abi_Close_0(IntPtr thisPtr) { - - try { global::WinRT.ComWrappersSupport.FindObject(thisPtr).Dispose(); diff --git a/src/WinRT.Runtime/TypeExtensions.cs b/src/WinRT.Runtime/TypeExtensions.cs index fab82c746..f0fc24237 100644 --- a/src/WinRT.Runtime/TypeExtensions.cs +++ b/src/WinRT.Runtime/TypeExtensions.cs @@ -1,62 +1,62 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -using System; -using System.Collections.Concurrent; +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using System; +using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; -using System.Reflection; - -namespace WinRT -{ -#if EMBED - internal -#else - public -#endif - static class TypeExtensions - { +using System.Reflection; + +namespace WinRT +{ +#if EMBED + internal +#else + public +#endif + static class TypeExtensions + { private readonly static ConcurrentDictionary HelperTypeCache = new ConcurrentDictionary(); -#if NET +#if NET [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | - DynamicallyAccessedMemberTypes.NonPublicMethods | - DynamicallyAccessedMemberTypes.PublicNestedTypes | - DynamicallyAccessedMemberTypes.PublicFields)] -#endif - public static Type FindHelperType(this Type type) - { + DynamicallyAccessedMemberTypes.NonPublicMethods | + DynamicallyAccessedMemberTypes.PublicNestedTypes | + DynamicallyAccessedMemberTypes.PublicFields)] +#endif + public static Type FindHelperType(this Type type) + { return HelperTypeCache.GetOrAdd(type, -#if NET +#if NET [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | - DynamicallyAccessedMemberTypes.NonPublicMethods | - DynamicallyAccessedMemberTypes.PublicNestedTypes | - DynamicallyAccessedMemberTypes.PublicFields)] -#endif - (type) => - { - if (typeof(Exception).IsAssignableFrom(type)) - { - type = typeof(Exception); - } - Type customMapping = Projections.FindCustomHelperTypeMapping(type); - if (customMapping is not null) - { - return customMapping; - } - - var helperTypeAtribute = type.GetCustomAttribute(); + DynamicallyAccessedMemberTypes.NonPublicMethods | + DynamicallyAccessedMemberTypes.PublicNestedTypes | + DynamicallyAccessedMemberTypes.PublicFields)] +#endif + (type) => + { + if (typeof(Exception).IsAssignableFrom(type)) + { + type = typeof(Exception); + } + Type customMapping = Projections.FindCustomHelperTypeMapping(type); + if (customMapping is not null) + { + return customMapping; + } + + var helperTypeAtribute = type.GetCustomAttribute(); if (helperTypeAtribute is not null) { return GetHelperTypeFromAttribute(helperTypeAtribute, type); - } - - return FindHelperTypeFallback(type); + } + + return FindHelperTypeFallback(type); }); #if NET [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "No members of the generic type are dynamically accessed other than for the attributes on it.")] - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.PublicFields)] + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.PublicFields)] #endif static Type GetHelperTypeFromAttribute(WindowsRuntimeHelperTypeAttribute helperTypeAtribute, Type type) { @@ -86,107 +86,112 @@ static Type FindHelperTypeFallback(Type type) var helper = $"ABI.{fullTypeName}"; return type.Assembly.GetType(helper) ?? Type.GetType(helper); - } + } } -#if NET - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | - DynamicallyAccessedMemberTypes.NonPublicMethods | - DynamicallyAccessedMemberTypes.PublicNestedTypes | - DynamicallyAccessedMemberTypes.PublicFields)] -#endif - public static Type GetHelperType(this Type type) - { - var helperType = type.FindHelperType(); - if (helperType is object) - return helperType; - throw new InvalidOperationException($"Target type is not a projected type: {type.FullName}."); +#if NET + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | + DynamicallyAccessedMemberTypes.NonPublicMethods | + DynamicallyAccessedMemberTypes.PublicNestedTypes | + DynamicallyAccessedMemberTypes.PublicFields)] +#endif + public static Type GetHelperType(this Type type) + { + var helperType = type.FindHelperType(); + if (helperType is object) + return helperType; + throw new InvalidOperationException($"Target type is not a projected type: {type.FullName}."); } -#if NET - [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] -#endif - public static Type GetGuidType(this Type type) - { - return type.IsDelegate() ? type.GetHelperType() : type; +#if NET + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] +#endif + public static Type GetGuidType(this Type type) + { + return type.IsDelegate() ? type.GetHelperType() : type; } #if NET [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)] -#endif +#endif public static Type FindVftblType( #if NET [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)] -#endif - this Type helperType) - { - Type vftblType = helperType.GetNestedType("Vftbl"); - if (vftblType is null) - { - return null; - } - if (helperType.IsGenericType && vftblType is object) - { - vftblType = vftblType.MakeGenericType(helperType.GetGenericArguments()); - } - return vftblType; - } - - internal static IntPtr GetAbiToProjectionVftblPtr( +#endif + this Type helperType) + { + Type vftblType = helperType.GetNestedType("Vftbl"); + if (vftblType is null) + { + return null; + } + if (helperType.IsGenericType && vftblType is object) + { + vftblType = vftblType.MakeGenericType(helperType.GetGenericArguments()); + } + return vftblType; + } + + internal static IntPtr GetAbiToProjectionVftblPtr( #if NET [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] -#endif - this Type helperType) - { - return (IntPtr)(helperType.FindVftblType() ?? helperType).GetField("AbiToProjectionVftablePtr", BindingFlags.Public | BindingFlags.Static).GetValue(null); - } - - public static Type GetAbiType(this Type type) - { - return type.GetHelperType().GetMethod("GetAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType; - } - - public static Type GetMarshalerType(this Type type) +#endif + this Type helperType) + { + return (IntPtr)(helperType.FindVftblType() ?? helperType).GetField("AbiToProjectionVftablePtr", BindingFlags.Public | BindingFlags.Static).GetValue(null); + } + + public static Type GetAbiType(this Type type) { - return type.GetHelperType().GetMethod("CreateMarshaler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType; + return type.GetHelperType().GetMethod("GetAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType; } - internal static Type GetMarshaler2Type(this Type type) + public static Type GetMarshalerType(this Type type) + { + return type.GetHelperType().GetMethod("CreateMarshaler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType; + } + + internal static Type GetMarshaler2Type(this Type type) { var helperType = type.GetHelperType(); var createMarshaler = helperType.GetMethod("CreateMarshaler2", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) ?? helperType.GetMethod("CreateMarshaler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); return createMarshaler.ReturnType; } - - internal static Type GetMarshalerArrayType(this Type type) - { - return type.GetHelperType().GetMethod("CreateMarshalerArray", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)?.ReturnType; - } - - public static bool IsDelegate(this Type type) - { - return typeof(Delegate).IsAssignableFrom(type); - } - - internal static bool IsTypeOfType(this Type type) - { - return typeof(Type).IsAssignableFrom(type); - } - - public static Type GetRuntimeClassCCWType(this Type type) - { - return type.IsClass && !type.IsArray ? type.GetAuthoringMetadataType() : null; + + internal static Type GetMarshalerArrayType(this Type type) + { + return type.GetHelperType().GetMethod("CreateMarshalerArray", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)?.ReturnType; } - private readonly static ConcurrentDictionary AuthoringMetadataTypeCache = new ConcurrentDictionary(); - internal static Type GetAuthoringMetadataType(this Type type) - { - return AuthoringMetadataTypeCache.GetOrAdd(type, (type) => - { - var ccwTypeName = $"ABI.Impl.{type.FullName}"; - return type.Assembly.GetType(ccwTypeName, false); - }); - } - } -} + public static bool IsDelegate(this Type type) + { + return typeof(Delegate).IsAssignableFrom(type); + } + + internal static bool IsTypeOfType(this Type type) + { + return typeof(Type).IsAssignableFrom(type); + } + + public static Type GetRuntimeClassCCWType(this Type type) + { + return type.IsClass && !type.IsArray ? type.GetAuthoringMetadataType() : null; + } + + private readonly static ConcurrentDictionary AuthoringMetadataTypeCache = new ConcurrentDictionary(); + + internal static Type GetAuthoringMetadataType(this Type type) + { + return AuthoringMetadataTypeCache.GetOrAdd(type, +#if NET + [RequiresUnreferencedCodeAttribute("If authoring a WinRT component in C# using C#/WinRT authoring support, it might require ABI helper types that might get trimmed. Avoid marking such components trimmable or ensure types don't get trimmed from it.")] +#endif + (type) => + { + var ccwTypeName = $"ABI.Impl.{type.FullName}"; + return type.Assembly.GetType(ccwTypeName, false); + }); + } + } +}