Skip to content

Commit c478ccc

Browse files
Fix #3319: KeyDownEvent field reference was replaced with KeyDown event reference.
1 parent 4cd4d58 commit c478ccc

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeMemberTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,4 +925,15 @@ public class T39_C_HideEvent : T39_B_HideEvent
925925
{
926926
public override event EventHandler E;
927927
}
928+
929+
public class T40_EventVsField
930+
{
931+
public object KeyDownEvent;
932+
private event EventHandler KeyDown;
933+
934+
public void UseObject()
935+
{
936+
Console.WriteLine(KeyDownEvent);
937+
}
938+
}
928939
}

ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ Identifier ReplaceEventFieldAnnotation(Identifier identifier)
761761
var parent = identifier.Parent;
762762
var mrr = parent.Annotation<MemberResolveResult>();
763763
var field = mrr?.Member as IField;
764-
if (field == null)
764+
if (field == null || field.Accessibility != Accessibility.Private)
765765
return null;
766766
foreach (var ev in field.DeclaringType.GetEvents(null, GetMemberOptions.IgnoreInheritedMembers))
767767
{
@@ -999,7 +999,9 @@ EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev)
999999
if (!ev.PrivateImplementationType.IsNull)
10001000
return null;
10011001
const Modifiers withoutBody = Modifiers.Abstract | Modifiers.Extern;
1002-
if ((ev.Modifiers & withoutBody) == 0 && ev.GetSymbol() is IEvent symbol)
1002+
if (ev.GetSymbol() is not IEvent symbol)
1003+
return null;
1004+
if ((ev.Modifiers & withoutBody) == 0)
10031005
{
10041006
if (!CheckAutomaticEventV4AggressivelyInlined(ev) && !CheckAutomaticEventV4(ev) && !CheckAutomaticEventV2(ev) && !CheckAutomaticEventV4MCS(ev))
10051007
return null;
@@ -1018,7 +1020,7 @@ EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev)
10181020
ed.CopyAnnotationsFrom(ev);
10191021

10201022
var fieldDecl = ev.Parent?.Children.OfType<FieldDeclaration>()
1021-
.FirstOrDefault(fd => CSharpDecompiler.IsEventBackingFieldName(fd.Variables.Single().Name, ev.Name, out _));
1023+
.FirstOrDefault(IsEventBackingField);
10221024
if (fieldDecl != null)
10231025
{
10241026
fieldDecl.Remove();
@@ -1033,6 +1035,17 @@ EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev)
10331035

10341036
ev.ReplaceWith(ed);
10351037
return ed;
1038+
1039+
bool IsEventBackingField(FieldDeclaration fd)
1040+
{
1041+
if (fd.Variables.Count > 1)
1042+
return false;
1043+
if (fd.GetSymbol() is not IField f)
1044+
return false;
1045+
return f.Accessibility == Accessibility.Private
1046+
&& symbol.ReturnType.Equals(f.ReturnType)
1047+
&& CSharpDecompiler.IsEventBackingFieldName(f.Name, ev.Name, out _);
1048+
}
10361049
}
10371050
#endregion
10381051

0 commit comments

Comments
 (0)