From fa137e58e7e86fdcae37dc85ed8dae04536cb2d3 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Sun, 5 May 2024 21:29:03 +0300 Subject: [PATCH] Added a way to identify auto-generated members See AvaloniaUI/Avalonia#15616 --- src/HotAvalonia/AvaloniaRuntimeXamlScanner.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/HotAvalonia/AvaloniaRuntimeXamlScanner.cs b/src/HotAvalonia/AvaloniaRuntimeXamlScanner.cs index c2fe49e..b1cd347 100644 --- a/src/HotAvalonia/AvaloniaRuntimeXamlScanner.cs +++ b/src/HotAvalonia/AvaloniaRuntimeXamlScanner.cs @@ -1,3 +1,4 @@ +using System.CodeDom.Compiler; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Reflection.Emit; @@ -374,7 +375,8 @@ private static IEnumerable ExtractAvaloniaControls(ReadOnly return userControlType .GetMethods(InstanceMember) - .OrderByDescending(static x => x.GetParameters().Length) + .OrderByDescending(static x => x.IsGeneratedByAvalonia()) + .ThenByDescending(static x => x.GetParameters().Length) .FirstOrDefault(static x => x.Name.Equals(initializeComponentMethodName, StringComparison.Ordinal) && x.ReturnType == typeof(void)); @@ -530,4 +532,25 @@ internal static IEnumerable FindDynamicPopulateMethods(string uri) yield return populateMethod; } } + + /// + /// Determines whether the specified member is generated by Avalonia. + /// + /// The member to check. + /// + /// true if the specified member is generated by Avalonia; + /// otherwise, false. + /// + internal static bool IsGeneratedByAvalonia(this MemberInfo member) + { + const string avaloniaGeneratorNamePrefix = "Avalonia.Generators."; + + _ = member ?? throw new ArgumentNullException(nameof(member)); + + GeneratedCodeAttribute? generatedCodeAttribute = member.GetCustomAttribute(); + if (generatedCodeAttribute is not { Tool: not null }) + return false; + + return generatedCodeAttribute.Tool.StartsWith(avaloniaGeneratorNamePrefix, StringComparison.Ordinal); + } }