Browse Source

Fix #2534: Handle default implementations of properties and events in interfaces.

pull/2536/head
Siegfried Pammer 4 years ago
parent
commit
ee3aabdc51
  1. 26
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/InterfaceTests.cs
  2. 9
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  3. 2
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

26
ICSharpCode.Decompiler.Tests/TestCases/Pretty/InterfaceTests.cs

@ -67,9 +67,35 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public interface IA2 : IA public interface IA2 : IA
{ {
#if CS80 #if CS80
int IA.Property3 {
get {
return 0;
}
set {
}
}
event EventHandler IA.MyEvent {
add {
}
remove {
}
}
new event EventHandler MyEvent {
add {
}
remove {
}
}
void IA.InternalMethod() void IA.InternalMethod()
{ {
} }
new void Method()
{
}
#endif #endif
} }
public interface IB public interface IB

9
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -1857,16 +1857,19 @@ namespace ICSharpCode.Decompiler.CSharp
var watch = System.Diagnostics.Stopwatch.StartNew(); var watch = System.Diagnostics.Stopwatch.StartNew();
try try
{ {
bool adderHasBody = ev.CanAdd && ev.AddAccessor.HasBody;
bool removerHasBody = ev.CanRemove && ev.RemoveAccessor.HasBody;
var typeSystemAstBuilder = CreateAstBuilder(decompileRun.Settings); var typeSystemAstBuilder = CreateAstBuilder(decompileRun.Settings);
typeSystemAstBuilder.UseCustomEvents = ev.DeclaringTypeDefinition.Kind != TypeKind.Interface; typeSystemAstBuilder.UseCustomEvents = ev.DeclaringTypeDefinition.Kind != TypeKind.Interface
|| ev.IsExplicitInterfaceImplementation
|| adderHasBody
|| removerHasBody;
var eventDecl = typeSystemAstBuilder.ConvertEntity(ev); var eventDecl = typeSystemAstBuilder.ConvertEntity(ev);
int lastDot = ev.Name.LastIndexOf('.'); int lastDot = ev.Name.LastIndexOf('.');
if (ev.IsExplicitInterfaceImplementation) if (ev.IsExplicitInterfaceImplementation)
{ {
eventDecl.Name = ev.Name.Substring(lastDot + 1); eventDecl.Name = ev.Name.Substring(lastDot + 1);
} }
bool adderHasBody = ev.CanAdd && ev.AddAccessor.HasBody;
bool removerHasBody = ev.CanRemove && ev.RemoveAccessor.HasBody;
if (adderHasBody) if (adderHasBody)
{ {
DecompileBody(ev.AddAccessor, ((CustomEventDeclaration)eventDecl).AddAccessor, decompileRun, decompilationContext); DecompileBody(ev.AddAccessor, ((CustomEventDeclaration)eventDecl).AddAccessor, decompileRun, decompilationContext);

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

@ -992,7 +992,7 @@ 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 && symbol.DeclaringType.Kind != TypeKind.Interface) if ((ev.Modifiers & withoutBody) == 0 && ev.GetSymbol() is IEvent symbol)
{ {
if (!CheckAutomaticEventV4AggressivelyInlined(ev) && !CheckAutomaticEventV4(ev) && !CheckAutomaticEventV2(ev) && !CheckAutomaticEventV4MCS(ev)) if (!CheckAutomaticEventV4AggressivelyInlined(ev) && !CheckAutomaticEventV4(ev) && !CheckAutomaticEventV2(ev) && !CheckAutomaticEventV4MCS(ev))
return null; return null;

Loading…
Cancel
Save