diff --git a/src/Avalonia.Base/Avalonia.Base.csproj b/src/Avalonia.Base/Avalonia.Base.csproj index 1dbfe9fd8e..4ffd9de1b6 100644 --- a/src/Avalonia.Base/Avalonia.Base.csproj +++ b/src/Avalonia.Base/Avalonia.Base.csproj @@ -32,6 +32,7 @@ + diff --git a/src/Avalonia.Base/Compatibility/StringSyntaxAttribute.cs b/src/Avalonia.Base/Compatibility/StringSyntaxAttribute.cs new file mode 100644 index 0000000000..2b3585fbe4 --- /dev/null +++ b/src/Avalonia.Base/Compatibility/StringSyntaxAttribute.cs @@ -0,0 +1,43 @@ +#pragma warning disable MA0048 // File name must match type name +// https://github.com/dotnet/runtime/blob/v8.0.4/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/StringSyntaxAttribute.cs + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// ReSharper disable once CheckNamespace +namespace System.Diagnostics.CodeAnalysis +{ +#if !NET7_0_OR_GREATER + /// Specifies the syntax used in a string. + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] + internal sealed class StringSyntaxAttribute : Attribute + { + /// Initializes the with the identifier of the syntax used. + /// The syntax identifier. + public StringSyntaxAttribute(string syntax) + { + Syntax = syntax; + Arguments = Array.Empty(); + } + + /// Initializes the with the identifier of the syntax used. + /// The syntax identifier. + /// Optional arguments associated with the specific syntax employed. + public StringSyntaxAttribute(string syntax, params object?[] arguments) + { + Syntax = syntax; + Arguments = arguments; + } + + /// Gets the identifier of the syntax used. + public string Syntax { get; } + + /// Optional arguments associated with the specific syntax employed. + public object?[] Arguments { get; } + + /// The syntax identifier for strings containing XML. + public const string Xml = nameof(Xml); + } +#endif +} diff --git a/src/Avalonia.Base/Compatibility/TrimmingAttributes.cs b/src/Avalonia.Base/Compatibility/TrimmingAttributes.cs index 941faa46bc..8adc6f6664 100644 --- a/src/Avalonia.Base/Compatibility/TrimmingAttributes.cs +++ b/src/Avalonia.Base/Compatibility/TrimmingAttributes.cs @@ -7,7 +7,6 @@ namespace System.Diagnostics.CodeAnalysis { -#nullable enable #if !NET6_0_OR_GREATER [AttributeUsage( AttributeTargets.Field | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaRuntimeXamlLoader.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaRuntimeXamlLoader.cs index 0368dea92f..eb153f7d0c 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaRuntimeXamlLoader.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaRuntimeXamlLoader.cs @@ -22,7 +22,7 @@ namespace Avalonia.Markup.Xaml /// Indicates whether the XAML is being loaded in design mode. /// The loaded object. [RequiresUnreferencedCode(XamlX.TrimmingMessages.DynamicXamlReference)] - public static object Load(string xaml, Assembly? localAssembly = null, object? rootInstance = null, Uri? uri = null, bool designMode = false) + public static object Load([StringSyntax(StringSyntaxAttribute.Xml)] string xaml, Assembly? localAssembly = null, object? rootInstance = null, Uri? uri = null, bool designMode = false) { xaml = xaml ?? throw new ArgumentNullException(nameof(xaml)); @@ -74,7 +74,7 @@ namespace Avalonia.Markup.Xaml /// Default assembly for clr-namespace:. /// The loaded object. [RequiresUnreferencedCode(XamlX.TrimmingMessages.DynamicXamlReference)] - public static object Parse(string xaml, Assembly? localAssembly = null) + public static object Parse([StringSyntax(StringSyntaxAttribute.Xml)] string xaml, Assembly? localAssembly = null) => Load(xaml, localAssembly); /// @@ -85,7 +85,7 @@ namespace Avalonia.Markup.Xaml /// >Default assembly for clr-namespace:. /// The loaded object. [RequiresUnreferencedCode(XamlX.TrimmingMessages.DynamicXamlReference)] - public static T Parse<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string xaml, Assembly? localAssembly = null) + public static T Parse<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>([StringSyntax(StringSyntaxAttribute.Xml)] string xaml, Assembly? localAssembly = null) => (T)Parse(xaml, localAssembly); } } diff --git a/src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderDocument.cs b/src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderDocument.cs index 4f6b1c4eef..5fabc6ee35 100644 --- a/src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderDocument.cs +++ b/src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderDocument.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; @@ -6,24 +7,24 @@ namespace Avalonia.Markup.Xaml; public class RuntimeXamlLoaderDocument { - public RuntimeXamlLoaderDocument(string xaml) + public RuntimeXamlLoaderDocument([StringSyntax(StringSyntaxAttribute.Xml)] string xaml) { XamlStream = new MemoryStream(Encoding.UTF8.GetBytes(xaml)); } - public RuntimeXamlLoaderDocument(Uri? baseUri, string xaml) + public RuntimeXamlLoaderDocument(Uri? baseUri, [StringSyntax(StringSyntaxAttribute.Xml)] string xaml) : this(xaml) { BaseUri = baseUri; } - public RuntimeXamlLoaderDocument(object? rootInstance, string xaml) + public RuntimeXamlLoaderDocument(object? rootInstance, [StringSyntax(StringSyntaxAttribute.Xml)] string xaml) : this(xaml) { RootInstance = rootInstance; } - public RuntimeXamlLoaderDocument(Uri? baseUri, object? rootInstance, string xaml) + public RuntimeXamlLoaderDocument(Uri? baseUri, object? rootInstance, [StringSyntax(StringSyntaxAttribute.Xml)] string xaml) : this(baseUri, xaml) { RootInstance = rootInstance;