From 4f4c3cddfd53df5a3d8d963215396dcdebb5fbde Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Fri, 17 Jan 2025 02:01:05 +0000 Subject: [PATCH 1/3] Add AOT Markup --- ...rovalTests.SplatUIProject.DotNet8_0.verified.txt | 2 ++ ...rovalTests.SplatUIProject.DotNet9_0.verified.txt | 2 ++ src/Splat.Drawing/Colors/SplatColor.cs | 4 ++++ src/Splat.Drawing/DefaultPlatformModeDetector.cs | 10 +++++++--- .../Android/Bitmaps/PlatformBitmapLoader.cs | 13 ++++++++++++- .../ServiceLocationDrawingInitialization.cs | 8 +++++++- src/Splat.Drawing/Splat.Drawing.csproj | 4 ++++ src/Splat/AssemblyFinder.cs | 9 ++++++++- src/Splat/Splat.csproj | 7 ++++++- 9 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/Splat.Drawing.Tests/API/ApiApprovalTests.SplatUIProject.DotNet8_0.verified.txt b/src/Splat.Drawing.Tests/API/ApiApprovalTests.SplatUIProject.DotNet8_0.verified.txt index 8d93e0c7f..7506df6b0 100644 --- a/src/Splat.Drawing.Tests/API/ApiApprovalTests.SplatUIProject.DotNet8_0.verified.txt +++ b/src/Splat.Drawing.Tests/API/ApiApprovalTests.SplatUIProject.DotNet8_0.verified.txt @@ -260,6 +260,8 @@ namespace Splat } public static class ServiceLocationDrawingInitialization { + [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Calls IMutableDependencyResolver.RegisterLazySingleton(Func)")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Calls IMutableDependencyResolver.RegisterLazySingleton(Func)")] public static void RegisterPlatformBitmapLoader(this Splat.IMutableDependencyResolver resolver) { } } public static class SizeExtensions diff --git a/src/Splat.Drawing.Tests/API/ApiApprovalTests.SplatUIProject.DotNet9_0.verified.txt b/src/Splat.Drawing.Tests/API/ApiApprovalTests.SplatUIProject.DotNet9_0.verified.txt index 400ee13a0..bd60e2859 100644 --- a/src/Splat.Drawing.Tests/API/ApiApprovalTests.SplatUIProject.DotNet9_0.verified.txt +++ b/src/Splat.Drawing.Tests/API/ApiApprovalTests.SplatUIProject.DotNet9_0.verified.txt @@ -260,6 +260,8 @@ namespace Splat } public static class ServiceLocationDrawingInitialization { + [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Calls IMutableDependencyResolver.RegisterLazySingleton(Func)")] + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Calls IMutableDependencyResolver.RegisterLazySingleton(Func)")] public static void RegisterPlatformBitmapLoader(this Splat.IMutableDependencyResolver resolver) { } } public static class SizeExtensions diff --git a/src/Splat.Drawing/Colors/SplatColor.cs b/src/Splat.Drawing/Colors/SplatColor.cs index 002d93ef8..b0a0f2d69 100644 --- a/src/Splat.Drawing/Colors/SplatColor.cs +++ b/src/Splat.Drawing/Colors/SplatColor.cs @@ -246,7 +246,11 @@ public static SplatColor FromName(string name) #pragma warning disable CA1031 // Do not catch general exception types try { +#if NET6_0_OR_GREATER + var kc = Enum.Parse(name, true); +#else KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), name, true); +#endif return FromKnownColor(kc); } catch (Exception ex) diff --git a/src/Splat.Drawing/DefaultPlatformModeDetector.cs b/src/Splat.Drawing/DefaultPlatformModeDetector.cs index d0388aedf..5d63db8b0 100644 --- a/src/Splat.Drawing/DefaultPlatformModeDetector.cs +++ b/src/Splat.Drawing/DefaultPlatformModeDetector.cs @@ -3,6 +3,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for full license information. +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Reflection; @@ -64,11 +65,14 @@ public class DefaultPlatformModeDetector : IPlatformModeDetector else { var designEnvironments = new[] { "BLEND.EXE", "XDESPROC.EXE" }; - - var entry = Assembly.GetEntryAssembly(); +#if NETSTANDARD || NETFRAMEWORK || TIZEN + var entry = Assembly.GetEntryAssembly()?.Location; +#else + var entry = System.AppContext.BaseDirectory; +#endif if (entry is not null) { - var exeName = new FileInfo(entry.Location).Name; + var exeName = new FileInfo(entry).Name; if (designEnvironments.Any(x => #if NETSTANDARD || NETFRAMEWORK || TIZEN diff --git a/src/Splat.Drawing/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs b/src/Splat.Drawing/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs index a59fe8d5a..c76257a2a 100644 --- a/src/Splat.Drawing/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs +++ b/src/Splat.Drawing/Platforms/Android/Bitmaps/PlatformBitmapLoader.cs @@ -3,7 +3,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for full license information. +using System.Diagnostics.CodeAnalysis; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text; using Android.App; @@ -21,6 +23,8 @@ public class PlatformBitmapLoader : IBitmapLoader, IEnableLogger /// /// Initializes a new instance of the class. /// + [RequiresUnreferencedCode("Calls Splat.PlatformBitmapLoader.GetDrawableList()")] + [RequiresDynamicCode("Calls Splat.PlatformBitmapLoader.GetDrawableList()")] public PlatformBitmapLoader() => _drawableList = GetDrawableList(); /// @@ -109,13 +113,16 @@ public class PlatformBitmapLoader : IBitmapLoader, IEnableLogger #pragma warning restore CA2000 // Dispose objects before losing scope } + [RequiresDynamicCode("Calls Splat.PlatformBitmapLoader.GetDrawableList(IFullLogger, Assembly[])")] + [RequiresUnreferencedCode("Calls Splat.PlatformBitmapLoader.GetDrawableList(IFullLogger, Assembly[])")] internal static Dictionary GetDrawableList(IFullLogger? log) { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - return GetDrawableList(log, assemblies); } + [RequiresDynamicCode("Reflection is required to get drawable resources.")] + [RequiresUnreferencedCode("Calls System.Reflection.Assembly.GetTypes()")] private static Type[] GetTypesFromAssembly( Assembly assembly, IFullLogger? log) @@ -153,6 +160,8 @@ private static Type[] GetTypesFromAssembly( } } + [RequiresDynamicCode("Reflection is required to get drawable resources.")] + [RequiresUnreferencedCode("Calls System.Reflection.Assembly.GetTypes()")] private static Dictionary GetDrawableList( IFullLogger? log, Assembly[] assemblies) @@ -220,6 +229,8 @@ private static bool HasCorrectStreamEnd(Stream sourceStream) && sourceStream.ReadByte() == 0xD9; } + [RequiresDynamicCode("Calls Splat.PlatformBitmapLoader.GetDrawableList(IFullLogger)")] + [RequiresUnreferencedCode("Calls Splat.PlatformBitmapLoader.GetDrawableList(IFullLogger)")] private static Dictionary GetDrawableList() => GetDrawableList(Locator.Current.GetService()?.GetLogger(typeof(PlatformBitmapLoader))); private void AttemptStreamByteCorrection(Stream sourceStream) diff --git a/src/Splat.Drawing/Platforms/ServiceLocationDrawingInitialization.cs b/src/Splat.Drawing/Platforms/ServiceLocationDrawingInitialization.cs index 85e2af930..f9331358b 100644 --- a/src/Splat.Drawing/Platforms/ServiceLocationDrawingInitialization.cs +++ b/src/Splat.Drawing/Platforms/ServiceLocationDrawingInitialization.cs @@ -3,6 +3,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for full license information. +using System.Diagnostics.CodeAnalysis; + namespace Splat; /// @@ -14,6 +16,10 @@ public static class ServiceLocationDrawingInitialization /// Registers the platform bitmap loader for the current platform. /// /// The resolver to register against. +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Calls IMutableDependencyResolver.RegisterLazySingleton(Func)")] + [RequiresDynamicCode("Calls IMutableDependencyResolver.RegisterLazySingleton(Func)")] +#endif public static void RegisterPlatformBitmapLoader(this IMutableDependencyResolver resolver) { resolver.ThrowArgumentNullExceptionIfNull(nameof(resolver)); @@ -22,7 +28,7 @@ public static void RegisterPlatformBitmapLoader(this IMutableDependencyResolver // not supported in netstandard or NET6 library if (!resolver.HasRegistration(typeof(IBitmapLoader))) { - resolver.RegisterLazySingleton(() => new PlatformBitmapLoader(), typeof(IBitmapLoader)); + resolver.RegisterLazySingleton(static () => new PlatformBitmapLoader(), typeof(IBitmapLoader)); } #endif } diff --git a/src/Splat.Drawing/Splat.Drawing.csproj b/src/Splat.Drawing/Splat.Drawing.csproj index c7a2d39d6..b267b1b67 100644 --- a/src/Splat.Drawing/Splat.Drawing.csproj +++ b/src/Splat.Drawing/Splat.Drawing.csproj @@ -9,6 +9,10 @@ $(NoWarn);1591 enable + + true + true + true true diff --git a/src/Splat/AssemblyFinder.cs b/src/Splat/AssemblyFinder.cs index 5464685e6..a8d780b8a 100644 --- a/src/Splat/AssemblyFinder.cs +++ b/src/Splat/AssemblyFinder.cs @@ -3,6 +3,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for full license information. +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace Splat; @@ -15,6 +16,10 @@ internal static class AssemblyFinder /// The type to cast the value to if we find it. /// The name of the full type. /// The created object or the default value. +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("Calls AssemblyFinder.AttemptToLoadType(string)")] + [RequiresDynamicCode("Calls AssemblyFinder.AttemptToLoadType(string)")] +#endif public static T? AttemptToLoadType(string fullTypeName) { var thisType = typeof(AssemblyFinder); @@ -39,13 +44,15 @@ internal static class AssemblyFinder foreach (var assembly in toSearch) { - var fullName = fullTypeName + ", " + assembly.FullName; + string fullName = fullTypeName + ", " + assembly.FullName; + var type = Type.GetType(fullName, false); if (type is null) { continue; } + GC.KeepAlive(type); return (T?)Activator.CreateInstance(type); } diff --git a/src/Splat/Splat.csproj b/src/Splat/Splat.csproj index e31c7585a..a443f79e0 100644 --- a/src/Splat/Splat.csproj +++ b/src/Splat/Splat.csproj @@ -8,5 +8,10 @@ Splat $(NoWarn);1591 - + + true + true + + + From a8569adc8dc77b941550c0f96f3220be4c7d40b2 Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Fri, 17 Jan 2025 04:31:46 +0000 Subject: [PATCH 2/3] Update Splat.Drawing.csproj --- src/Splat.Drawing/Splat.Drawing.csproj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Splat.Drawing/Splat.Drawing.csproj b/src/Splat.Drawing/Splat.Drawing.csproj index b267b1b67..c931cc9a0 100644 --- a/src/Splat.Drawing/Splat.Drawing.csproj +++ b/src/Splat.Drawing/Splat.Drawing.csproj @@ -11,7 +11,6 @@ true - true true @@ -92,4 +91,4 @@ - \ No newline at end of file + From 001d7b22a4f398d797cab9e7417f48505963f6b5 Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Fri, 17 Jan 2025 04:32:51 +0000 Subject: [PATCH 3/3] Update Splat.csproj --- src/Splat/Splat.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Splat/Splat.csproj b/src/Splat/Splat.csproj index a443f79e0..cbcf2b211 100644 --- a/src/Splat/Splat.csproj +++ b/src/Splat/Splat.csproj @@ -10,7 +10,6 @@ true - true