Skip to content

Commit

Permalink
Added a way to identify auto-generated members
Browse files Browse the repository at this point in the history
  • Loading branch information
Kir-Antipov committed May 5, 2024
1 parent 34ac4ce commit fa137e5
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/HotAvalonia/AvaloniaRuntimeXamlScanner.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.CodeDom.Compiler;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Reflection.Emit;
Expand Down Expand Up @@ -374,7 +375,8 @@ private static IEnumerable<AvaloniaControlInfo> 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));
Expand Down Expand Up @@ -530,4 +532,25 @@ internal static IEnumerable<MethodInfo> FindDynamicPopulateMethods(string uri)
yield return populateMethod;
}
}

/// <summary>
/// Determines whether the specified member is generated by Avalonia.
/// </summary>
/// <param name="member">The member to check.</param>
/// <returns>
/// <c>true</c> if the specified member is generated by Avalonia;
/// otherwise, <c>false</c>.
/// </returns>
internal static bool IsGeneratedByAvalonia(this MemberInfo member)
{
const string avaloniaGeneratorNamePrefix = "Avalonia.Generators.";

_ = member ?? throw new ArgumentNullException(nameof(member));

GeneratedCodeAttribute? generatedCodeAttribute = member.GetCustomAttribute<GeneratedCodeAttribute>();
if (generatedCodeAttribute is not { Tool: not null })
return false;

return generatedCodeAttribute.Tool.StartsWith(avaloniaGeneratorNamePrefix, StringComparison.Ordinal);
}
}

0 comments on commit fa137e5

Please sign in to comment.