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..c931cc9a0 100644 --- a/src/Splat.Drawing/Splat.Drawing.csproj +++ b/src/Splat.Drawing/Splat.Drawing.csproj @@ -9,6 +9,9 @@ $(NoWarn);1591 enable + + true + true true @@ -88,4 +91,4 @@ - \ No newline at end of file + 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..cbcf2b211 100644 --- a/src/Splat/Splat.csproj +++ b/src/Splat/Splat.csproj @@ -8,5 +8,9 @@ Splat $(NoWarn);1591 - + + true + + +