Browse Source

Fix #3319: KeyDownEvent field reference was replaced with KeyDown event reference.

pull/3335/head
Siegfried Pammer 6 months ago
parent
commit
c478ccc2e0
  1. 11
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/TypeMemberTests.cs
  2. 19
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

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

@ -925,4 +925,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{ {
public override event EventHandler E; public override event EventHandler E;
} }
public class T40_EventVsField
{
public object KeyDownEvent;
private event EventHandler KeyDown;
public void UseObject()
{
Console.WriteLine(KeyDownEvent);
}
}
} }

19
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -761,7 +761,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
var parent = identifier.Parent; var parent = identifier.Parent;
var mrr = parent.Annotation<MemberResolveResult>(); var mrr = parent.Annotation<MemberResolveResult>();
var field = mrr?.Member as IField; var field = mrr?.Member as IField;
if (field == null) if (field == null || field.Accessibility != Accessibility.Private)
return null; return null;
foreach (var ev in field.DeclaringType.GetEvents(null, GetMemberOptions.IgnoreInheritedMembers)) foreach (var ev in field.DeclaringType.GetEvents(null, GetMemberOptions.IgnoreInheritedMembers))
{ {
@ -999,7 +999,9 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (!ev.PrivateImplementationType.IsNull) if (!ev.PrivateImplementationType.IsNull)
return null; return null;
const Modifiers withoutBody = Modifiers.Abstract | Modifiers.Extern; 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)) if (!CheckAutomaticEventV4AggressivelyInlined(ev) && !CheckAutomaticEventV4(ev) && !CheckAutomaticEventV2(ev) && !CheckAutomaticEventV4MCS(ev))
return null; return null;
@ -1018,7 +1020,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
ed.CopyAnnotationsFrom(ev); ed.CopyAnnotationsFrom(ev);
var fieldDecl = ev.Parent?.Children.OfType<FieldDeclaration>() var fieldDecl = ev.Parent?.Children.OfType<FieldDeclaration>()
.FirstOrDefault(fd => CSharpDecompiler.IsEventBackingFieldName(fd.Variables.Single().Name, ev.Name, out _)); .FirstOrDefault(IsEventBackingField);
if (fieldDecl != null) if (fieldDecl != null)
{ {
fieldDecl.Remove(); fieldDecl.Remove();
@ -1033,6 +1035,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
ev.ReplaceWith(ed); ev.ReplaceWith(ed);
return 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 #endregion

Loading…
Cancel
Save