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

Loading…
Cancel
Save