diff --git a/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs b/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs index acf90179b1..d11fd08343 100644 --- a/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs +++ b/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs @@ -157,6 +157,12 @@ namespace Avalonia.Build.Tasks var arg1 = new CustomAttributeArgument(strType, "AvaloniaUseCompiledBindingsByDefault"); var arg2 = new CustomAttributeArgument(strType, defaultCompileBindings.ToString()); asm.CustomAttributes.Add(new CustomAttribute(ctor) { ConstructorArguments = { arg1, arg2 } }); + if (createSourceInfo) + { + var arg3 = new CustomAttributeArgument(strType, "AvaloniaXamlCreateSourceInfo"); + var arg4 = new CustomAttributeArgument(strType, createSourceInfo.ToString()); + asm.CustomAttributes.Add(new CustomAttribute(ctor) { ConstructorArguments = { arg3, arg4 } }); + } } TypeDefinition AddClass(string name, TypeAttributes extraAttributes = 0) diff --git a/src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderConfiguration.cs b/src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderConfiguration.cs index c9c421b766..73713e5d5b 100644 --- a/src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderConfiguration.cs +++ b/src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderConfiguration.cs @@ -1,10 +1,16 @@ using System; +using System.Linq; using System.Reflection; namespace Avalonia.Markup.Xaml; public class RuntimeXamlLoaderConfiguration { + /// + /// When enabled, the XAML compiler embeds SourceInfo metadata (file path, line, and column) into generated code. + /// + private bool? _createSourceInfo = null; + /// /// Default assembly for clr-namespace:. /// @@ -24,9 +30,24 @@ public class RuntimeXamlLoaderConfiguration /// /// When enabled, the XAML compiler embeds SourceInfo metadata (file path, line, and column) into generated code. - /// Default is 'false'. + /// When not explicitly set, the 'AvaloniaXamlCreateSourceInfo' from the LocalAssembly will be used to determine the value. /// - public bool CreateSourceInfo { get; set; } = false; + public bool CreateSourceInfo + { + get { + if(_createSourceInfo == null) + { + if(LocalAssembly is {} asm) + { + var createSourceInfo = asm.GetCustomAttributes() + .FirstOrDefault(a => a.Key == "AvaloniaXamlCreateSourceInfo")?.Value; + return bool.TryParse(createSourceInfo, out var parsedCreateSourceInfo) && parsedCreateSourceInfo; + } + } + return _createSourceInfo ?? false; + } + set => _createSourceInfo = value; + } /// /// XAML diagnostics handler.