public static void TextElementFontSizePropertyAddOwner() { var code = @" namespace N { using System.Windows; using System.Windows.Documents; public class FooControl : FrameworkElement { public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(typeof(FooControl)); public double FontSize { get => (double)this.GetValue(FontSizeProperty); set => this.SetValue(FontSizeProperty, value); } } }"; var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var declaration = syntaxTree.FindFieldDeclaration("FontSizeProperty"); var symbol = semanticModel.GetDeclaredSymbolSafe(declaration, CancellationToken.None); var result = BackingFieldOrProperty.Match(symbol)?.RegisteredName(semanticModel, CancellationToken.None); Assert.AreEqual(null, result?.Argument); Assert.AreEqual("FontSize", result?.Value); }
public static void DependencyPropertyBackingProperty(string argument) { var code = @" namespace N { using System.Windows; using System.Windows.Controls; public class FooControl : Control { public static DependencyProperty BarProperty { get; } = DependencyProperty.Register( nameof(Bar), typeof(int), typeof(FooControl), new PropertyMetadata(default(int))); public int Bar { get { return (int)this.GetValue(BarProperty); } set { this.SetValue(BarProperty, value); } } } }".AssertReplace("nameof(Bar)", argument); var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var declaration = syntaxTree.FindPropertyDeclaration("BarProperty"); var symbol = semanticModel.GetDeclaredSymbol(declaration, CancellationToken.None); var result = BackingFieldOrProperty.Match(symbol)?.RegisteredName(semanticModel, CancellationToken.None); Assert.AreEqual(argument, result?.Argument?.ToString()); Assert.AreEqual("Bar", result?.Value); }