diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/PropertiesAndEvents.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/PropertiesAndEvents.cs index 5741b77bb..80e50c707 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/PropertiesAndEvents.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/PropertiesAndEvents.cs @@ -191,6 +191,12 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public event EventHandler AutomaticEventWithInitializer = delegate { }; +#if ROSLYN + // Legacy csc has a bug where EventHandler is only used for the backing field + public event EventHandler DynamicAutoEvent; + public event EventHandler<(int A, string B)> AutoEventWithTuple; +#endif + public event EventHandler CustomEvent { add { AutomaticEvent += value; diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs index b4758059d..f245e1222 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs @@ -810,16 +810,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms default: return false; } - if (!ev.ReturnType.IsMatch(m.Get("type").Single())) { - // Variable types must match event type, - // except that the event type may have an additional nullability annotation - if (ev.ReturnType is ComposedType ct && ct.HasOnlyNullableSpecifier) { - if (!ct.BaseType.IsMatch(m.Get("type").Single())) - return false; - } else { - return false; - } - } + var returnType = ev.ReturnType.GetResolveResult().Type; + var eventType = m.Get("type").Single().GetResolveResult().Type; + // ignore tuple element names, dynamic and nullability + if (!NormalizeTypeVisitor.TypeErasure.EquivalentTypes(returnType, eventType)) + return false; var combineMethod = m.Get("delegateCombine").Single().Parent.GetSymbol() as IMethod; if (combineMethod == null || combineMethod.Name != (isAddAccessor ? "Combine" : "Remove")) return false; @@ -889,9 +884,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms ed.Modifiers = ev.Modifiers; ed.Variables.Add(new VariableInitializer(ev.Name)); ed.CopyAnnotationsFrom(ev); - - IEvent eventDef = ev.GetSymbol() as IEvent; - if (eventDef != null) { + + if (ev.GetSymbol() is IEvent eventDef) { IField field = eventDef.DeclaringType.GetFields(f => f.Name == ev.Name, GetMemberOptions.IgnoreInheritedMembers).SingleOrDefault(); if (field != null) { ed.AddAnnotation(field); @@ -907,7 +901,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms } } } - + ev.ReplaceWith(ed); return ed; }