public static void WhenCachingInConcurrentDictionaryTempLocal(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>(); private int bar; public event PropertyChangedEventHandler PropertyChanged; public int Bar { get { return this.bar; } set { if (value == this.bar) { return; } this.bar = value; this.OnPropertyChanged(); } } 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 invocation = syntaxTree.FindInvocation("this.OnPropertyChanged();"); Assert.AreEqual(AnalysisResult.Yes, PropertyChanged.TryGetName(invocation, semanticModel, CancellationToken.None, out var name)); Assert.AreEqual("Bar", name); }
public static void WhenTrue(string call) { var syntaxTree = CSharpSyntaxTree.ParseText( @" namespace N { using System; using System.ComponentModel; using System.Linq.Expressions; using System.Runtime.CompilerServices; public class Foo : INotifyPropertyChanged { private static readonly PropertyChangedEventArgs Cached = new PropertyChangedEventArgs(""Bar""); private int bar; public event PropertyChangedEventHandler PropertyChanged; public int Bar { get { return this.bar; } set { if (value == this.bar) { return; } this.bar = value; this.OnPropertyChanged(); this.OnPropertyChanged(""Bar""); this.OnPropertyChanged(nameof(Bar)); this.OnPropertyChanged(nameof(this.Bar)); this.OnPropertyChanged(() => Bar); this.OnPropertyChanged(() => this.Bar); this.OnPropertyChanged(new PropertyChangedEventArgs(""Bar"")); this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Bar))); this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(this.Bar))); this.OnPropertyChanged(Cached); var args = new PropertyChangedEventArgs(""Bar""); this.OnPropertyChanged(args); } } protected virtual void OnPropertyChanged<T>(Expression<Func<T>> property) { this.OnPropertyChanged(((MemberExpression)property.Body).Member.Name); } 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 invocation = syntaxTree.FindInvocation(call); Assert.AreEqual(call, invocation.ToString()); Assert.AreEqual(AnalysisResult.Yes, PropertyChanged.TryGetName(invocation, semanticModel, CancellationToken.None, out var name)); Assert.AreEqual("Bar", name); }