Example #1
0
            public static void PropertyChangedEventArgsBeforeCallerMemberName()
            {
                var syntaxTree = CSharpSyntaxTree.ParseText(
                    @"
namespace N
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    public class C : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int Bar { get; set; }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            this.PropertyChanged?.Invoke(this, e);
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}");
                var compilation      = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
                var semanticModel    = compilation.GetSemanticModel(syntaxTree);
                var classDeclaration = syntaxTree.FindClassDeclaration("C");
                var type             = semanticModel.GetDeclaredSymbol(classDeclaration);

                Assert.AreEqual(true, OnPropertyChanged.TryFind(type, semanticModel, CancellationToken.None, out var invoker));
                Assert.AreEqual("N.C.OnPropertyChanged(string)", invoker.ToString());
            }
Example #2
0
            public static void CachingInConcurrentDictionaryLocal(string expression)
            {
                var syntaxTree = CSharpSyntaxTree.ParseText(
                    @"
namespace N
{
    using System.Collections.Concurrent;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    public class C : INotifyPropertyChanged
    {
        private static readonly ConcurrentDictionary<string, PropertyChangedEventArgs> Cache = new ConcurrentDictionary<string, PropertyChangedEventArgs>();

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var e = Cache.GetOrAdd(propertyName ?? string.Empty, name => new PropertyChangedEventArgs(name));
            this.PropertyChanged?.Invoke(this, e);
        }
    }
}".AssertReplace("propertyName ?? string.Empty", expression));
                var compilation      = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
                var semanticModel    = compilation.GetSemanticModel(syntaxTree);
                var classDeclaration = syntaxTree.FindClassDeclaration("C");
                var type             = semanticModel.GetDeclaredSymbol(classDeclaration);

                Assert.AreEqual(true, OnPropertyChanged.TryFind(type, semanticModel, CancellationToken.None, out var invoker));
                Assert.AreEqual("N.C.OnPropertyChanged(string)", invoker.ToString());
            }
Example #3
0
            public static void WhenCreatingPropertyChangedEventArgsSeparately()
            {
                var syntaxTree = CSharpSyntaxTree.ParseText(@"
namespace N
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    public class C : INotifyPropertyChanged
    {
        private int bar;

        public event PropertyChangedEventHandler PropertyChanged;

        public int Bar
        {
            get => this.bar;

            set
            {
                if (value == this.bar)
                {
                    return;
                }

                this.bar = value;
                this.OnPropertyChanged();
            }
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                var args = new PropertyChangedEventArgs(propertyName);
                handler.Invoke(this, args);
            }
        }
    }
}");

                var compilation      = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
                var semanticModel    = compilation.GetSemanticModel(syntaxTree);
                var classDeclaration = syntaxTree.FindClassDeclaration("C");
                var type             = semanticModel.GetDeclaredSymbol(classDeclaration);

                Assert.AreEqual(true, OnPropertyChanged.TryFind(type, semanticModel, CancellationToken.None, out var invoker));
                Assert.AreEqual("N.C.OnPropertyChanged(string)", invoker.ToString());
            }
Example #4
0
            public static void OverridingEvent()
            {
                var viewModelBaseCode = CSharpSyntaxTree.ParseText(@"
namespace N
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    public class ViewModelBase : INotifyPropertyChanged
    {
        public virtual event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}");

                var code = CSharpSyntaxTree.ParseText(@"
namespace N
{
    using System.ComponentModel;

    public class C : N.ViewModelBase
    {
        public override event PropertyChangedEventHandler PropertyChanged;
    }
}");

                var compilation      = CSharpCompilation.Create("test", new[] { viewModelBaseCode, code }, MetadataReferences.FromAttributes());
                var semanticModel    = compilation.GetSemanticModel(code);
                var classDeclaration = code.FindClassDeclaration("ViewModel");
                var type             = semanticModel.GetDeclaredSymbol(classDeclaration);

                Assert.AreEqual(true, OnPropertyChanged.TryFind(type, semanticModel, CancellationToken.None, out var invoker));
                Assert.AreEqual("N.ViewModelBase.OnPropertyChanged(string)", invoker.ToString());
            }