Browse Source

Fix the issue where parameters in the base class constructor call are not passed correctly for inherited classes declared using the primary constructor form (e.g., the `DeserializationException` type in RestSharp), and also fix the subsequent issue where an extra parenthesis '()' is output if the base class is an interface (e.g., the `XmlRestSerializer` type in RestSharp).

pull/3598/head
sonyps5201314 6 months ago
parent
commit
554ec92e73
  1. 20
      ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

20
ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

@ -125,15 +125,21 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (context.DecompileRun.RecordDecompilers.TryGetValue(currentCtor.DeclaringTypeDefinition, out var record) if (context.DecompileRun.RecordDecompilers.TryGetValue(currentCtor.DeclaringTypeDefinition, out var record)
&& currentCtor.Equals(record.PrimaryConstructor)) && currentCtor.Equals(record.PrimaryConstructor))
{ {
if (record.IsInheritedRecord && if (ci?.ConstructorInitializerType == ConstructorInitializerType.Base &&
ci?.ConstructorInitializerType == ConstructorInitializerType.Base &&
constructorDeclaration.Parent is TypeDeclaration { BaseTypes: { Count: >= 1 } } typeDecl) constructorDeclaration.Parent is TypeDeclaration { BaseTypes: { Count: >= 1 } } typeDecl)
{ {
var baseType = typeDecl.BaseTypes.First(); // Only perform this transformation if the declaring type actually has a non-object class base.
var newBaseType = new InvocationAstType(); // This prevents converting implemented interfaces into an invocation type (which caused the
baseType.ReplaceWith(newBaseType); // spurious trailing '()' in the decompiled output).
newBaseType.BaseType = baseType; var baseClassType = currentCtor.DeclaringTypeDefinition.DirectBaseTypes.FirstOrDefault(b => b.Kind == TypeKind.Class);
ci.Arguments.MoveTo(newBaseType.Arguments); if (baseClassType != null && !baseClassType.IsKnownType(KnownTypeCode.Object))
{
var baseType = typeDecl.BaseTypes.First();
var newBaseType = new InvocationAstType();
baseType.ReplaceWith(newBaseType);
newBaseType.BaseType = baseType;
ci.Arguments.MoveTo(newBaseType.Arguments);
}
} }
if (constructorDeclaration.Parent is TypeDeclaration { PrimaryConstructorParameters: var parameters }) if (constructorDeclaration.Parent is TypeDeclaration { PrimaryConstructorParameters: var parameters })
{ {

Loading…
Cancel
Save