Skip to content

Commit

Permalink
Fix #3319: KeyDownEvent field reference was replaced with KeyDown eve…
Browse files Browse the repository at this point in the history
…nt reference.
  • Loading branch information
siegfriedpammer committed Nov 10, 2024
1 parent 4cd4d58 commit c478ccc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
11 changes: 11 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeMemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ Identifier ReplaceEventFieldAnnotation(Identifier identifier)
var parent = identifier.Parent;
var mrr = parent.Annotation<MemberResolveResult>();
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))
{
Expand Down Expand Up @@ -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;
Expand All @@ -1018,7 +1020,7 @@ EventDeclaration TransformAutomaticEvents(CustomEventDeclaration ev)
ed.CopyAnnotationsFrom(ev);

var fieldDecl = ev.Parent?.Children.OfType<FieldDeclaration>()
.FirstOrDefault(fd => CSharpDecompiler.IsEventBackingFieldName(fd.Variables.Single().Name, ev.Name, out _));
.FirstOrDefault(IsEventBackingField);
if (fieldDecl != null)
{
fieldDecl.Remove();
Expand All @@ -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

Expand Down

0 comments on commit c478ccc

Please sign in to comment.