From c478ccc2e0678c074561de2b76578e53fa0b0b3a Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 10 Nov 2024 22:07:30 +0100 Subject: [PATCH] Fix #3319: KeyDownEvent field reference was replaced with KeyDown event reference. --- .../TestCases/Pretty/TypeMemberTests.cs | 11 +++++++++++ .../Transforms/PatternStatementTransform.cs | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeMemberTests.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeMemberTests.cs index 4133229648..0bd6585867 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeMemberTests.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeMemberTests.cs @@ -925,4 +925,15 @@ public class T39_C_HideEvent : T39_B_HideEvent { public override event EventHandler E; } + + public class T40_EventVsField + { + public object KeyDownEvent; + private event EventHandler KeyDown; + + public void UseObject() + { + Console.WriteLine(KeyDownEvent); + } + } } diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs index 3605d968d7..b0f2375a28 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs @@ -761,7 +761,7 @@ Identifier ReplaceEventFieldAnnotation(Identifier identifier) var parent = identifier.Parent; var mrr = parent.Annotation(); var field = mrr?.Member as IField; - if (field == null) + if (field == null || field.Accessibility != Accessibility.Private) return null; foreach (var ev in field.DeclaringType.GetEvents(null, GetMemberOptions.IgnoreInheritedMembers)) { @@ -999,7 +999,9 @@ EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev) if (!ev.PrivateImplementationType.IsNull) return null; const Modifiers withoutBody = Modifiers.Abstract | Modifiers.Extern; - if ((ev.Modifiers & withoutBody) == 0 && ev.GetSymbol() is IEvent symbol) + if (ev.GetSymbol() is not IEvent symbol) + return null; + if ((ev.Modifiers & withoutBody) == 0) { if (!CheckAutomaticEventV4AggressivelyInlined(ev) && !CheckAutomaticEventV4(ev) && !CheckAutomaticEventV2(ev) && !CheckAutomaticEventV4MCS(ev)) return null; @@ -1018,7 +1020,7 @@ EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev) ed.CopyAnnotationsFrom(ev); var fieldDecl = ev.Parent?.Children.OfType() - .FirstOrDefault(fd => CSharpDecompiler.IsEventBackingFieldName(fd.Variables.Single().Name, ev.Name, out _)); + .FirstOrDefault(IsEventBackingField); if (fieldDecl != null) { fieldDecl.Remove(); @@ -1033,6 +1035,17 @@ EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev) ev.ReplaceWith(ed); return ed; + + bool IsEventBackingField(FieldDeclaration fd) + { + if (fd.Variables.Count > 1) + return false; + if (fd.GetSymbol() is not IField f) + return false; + return f.Accessibility == Accessibility.Private + && symbol.ReturnType.Equals(f.ReturnType) + && CSharpDecompiler.IsEventBackingFieldName(f.Name, ev.Name, out _); + } } #endregion