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);
        }
Example #3
0
            public void DependencyPropertyBackingProperty(string nameCode)
            {
                var testCode      = @"
namespace RoslynSandbox
{
    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)", nameCode);
                var syntaxTree    = CSharpSyntaxTree.ParseText(testCode);
                var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, RoslynAssert.MetadataReferences);
                var semanticModel = compilation.GetSemanticModel(syntaxTree);
                var declaration   = syntaxTree.FindPropertyDeclaration("BarProperty");
                var symbol        = semanticModel.GetDeclaredSymbol(declaration, CancellationToken.None);

                Assert.AreEqual(true, BackingFieldOrProperty.TryCreateForDependencyProperty(symbol, out var fieldOrProperty));
                Assert.AreEqual(true, DependencyProperty.TryGetRegisteredName(fieldOrProperty, semanticModel, CancellationToken.None, out var name));
                Assert.AreEqual("Bar", name);
            }
Example #4
0
            public void TextElementFontSizePropertyAddOwner()
            {
                var testCode      = @"
namespace RoslynSandbox
{
    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(testCode);
                var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, RoslynAssert.MetadataReferences);
                var semanticModel = compilation.GetSemanticModel(syntaxTree);
                var declaration   = syntaxTree.FindFieldDeclaration("FontSizeProperty");
                var symbol        = semanticModel.GetDeclaredSymbolSafe(declaration, CancellationToken.None);

                Assert.AreEqual(true, BackingFieldOrProperty.TryCreateForDependencyProperty(symbol, out var fieldOrProperty));
                Assert.AreEqual(true, DependencyProperty.TryGetRegisteredName(fieldOrProperty, semanticModel, CancellationToken.None, out var name));
                Assert.AreEqual("FontSize", name);
            }
Example #5
0
            public static void BorderBorderThicknessPropertyAddOwner()
            {
                var code          = @"
namespace N
{
    using System.Windows;
    using System.Windows.Controls;

    public class FooControl : FrameworkElement
    {
        public static readonly DependencyProperty BorderThicknessProperty = Border.BorderThicknessProperty.AddOwner(typeof(FooControl));

        public Size BorderThickness
        {
            get => (Size)GetValue(BorderThicknessProperty);
            set => SetValue(BorderThicknessProperty, value);
        }
    }
}";
                var syntaxTree    = CSharpSyntaxTree.ParseText(code);
                var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
                var semanticModel = compilation.GetSemanticModel(syntaxTree);
                var declaration   = syntaxTree.FindFieldDeclaration("BorderThicknessProperty");
                var symbol        = semanticModel.GetDeclaredSymbolSafe(declaration, CancellationToken.None);

                Assert.AreEqual(true, BackingFieldOrProperty.TryCreateForDependencyProperty(symbol, out var fieldOrProperty));
                Assert.AreEqual(true, DependencyProperty.TryGetRegisteredName(fieldOrProperty, semanticModel, CancellationToken.None, out var arg, out var name));
                Assert.AreEqual(null, arg);
                Assert.AreEqual("BorderThickness", name);
            }