Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AOT Markup #1263

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ namespace Splat
}
public static class ServiceLocationDrawingInitialization
{
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Calls IMutableDependencyResolver.RegisterLazySingleton<TService>(Func<TService>)")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Calls IMutableDependencyResolver.RegisterLazySingleton<TService>(Func<TService>)")]
public static void RegisterPlatformBitmapLoader(this Splat.IMutableDependencyResolver resolver) { }
}
public static class SizeExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ namespace Splat
}
public static class ServiceLocationDrawingInitialization
{
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Calls IMutableDependencyResolver.RegisterLazySingleton<TService>(Func<TService>)")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Calls IMutableDependencyResolver.RegisterLazySingleton<TService>(Func<TService>)")]
public static void RegisterPlatformBitmapLoader(this Splat.IMutableDependencyResolver resolver) { }
}
public static class SizeExtensions
Expand Down
4 changes: 4 additions & 0 deletions src/Splat.Drawing/Colors/SplatColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KnownColor>(name, true);
#else
KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), name, true);
#endif
return FromKnownColor(kc);
}
catch (Exception ex)
Expand Down
10 changes: 7 additions & 3 deletions src/Splat.Drawing/DefaultPlatformModeDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +23,8 @@ public class PlatformBitmapLoader : IBitmapLoader, IEnableLogger
/// <summary>
/// Initializes a new instance of the <see cref="PlatformBitmapLoader"/> class.
/// </summary>
[RequiresUnreferencedCode("Calls Splat.PlatformBitmapLoader.GetDrawableList()")]
[RequiresDynamicCode("Calls Splat.PlatformBitmapLoader.GetDrawableList()")]
public PlatformBitmapLoader() => _drawableList = GetDrawableList();

/// <inheritdoc />
Expand Down Expand Up @@ -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<string, int> 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)
Expand Down Expand Up @@ -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<string, int> GetDrawableList(
IFullLogger? log,
Assembly[] assemblies)
Expand Down Expand Up @@ -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<string, int> GetDrawableList() => GetDrawableList(Locator.Current.GetService<ILogManager>()?.GetLogger(typeof(PlatformBitmapLoader)));

private void AttemptStreamByteCorrection(Stream sourceStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -14,6 +16,10 @@ public static class ServiceLocationDrawingInitialization
/// Registers the platform bitmap loader for the current platform.
/// </summary>
/// <param name="resolver">The resolver to register against.</param>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Calls IMutableDependencyResolver.RegisterLazySingleton<TService>(Func<TService>)")]
[RequiresDynamicCode("Calls IMutableDependencyResolver.RegisterLazySingleton<TService>(Func<TService>)")]
#endif
public static void RegisterPlatformBitmapLoader(this IMutableDependencyResolver resolver)
{
resolver.ThrowArgumentNullExceptionIfNull(nameof(resolver));
Expand All @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion src/Splat.Drawing/Splat.Drawing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<NoWarn>$(NoWarn);1591</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('net8.0')) or $(TargetFramework.StartsWith('net9.0'))">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
<PropertyGroup Condition="($(TargetFramework.EndsWith('-windows10.0.17763.0')) or $(TargetFramework.StartsWith('net4'))) and '$(OS)' == 'Windows_NT'">
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
Expand Down Expand Up @@ -88,4 +91,4 @@
<ItemGroup>
<ProjectReference Include="..\Splat\Splat.csproj" />
</ItemGroup>
</Project>
</Project>
9 changes: 8 additions & 1 deletion src/Splat/AssemblyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,10 @@ internal static class AssemblyFinder
/// <typeparam name="T">The type to cast the value to if we find it.</typeparam>
/// <param name="fullTypeName">The name of the full type.</param>
/// <returns>The created object or the default value.</returns>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Calls AssemblyFinder.AttemptToLoadType<T>(string)")]
[RequiresDynamicCode("Calls AssemblyFinder.AttemptToLoadType<T>(string)")]
#endif
public static T? AttemptToLoadType<T>(string fullTypeName)
{
var thisType = typeof(AssemblyFinder);
Expand All @@ -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);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Splat/Splat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
<PackageId>Splat</PackageId>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework.StartsWith('net8.0')) or $(TargetFramework.StartsWith('net9.0'))">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

</Project>

Loading