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. 10
      ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

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

@ -125,9 +125,14 @@ 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)
{
// 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 baseType = typeDecl.BaseTypes.First();
var newBaseType = new InvocationAstType(); var newBaseType = new InvocationAstType();
@ -135,6 +140,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
newBaseType.BaseType = baseType; newBaseType.BaseType = baseType;
ci.Arguments.MoveTo(newBaseType.Arguments); ci.Arguments.MoveTo(newBaseType.Arguments);
} }
}
if (constructorDeclaration.Parent is TypeDeclaration { PrimaryConstructorParameters: var parameters }) if (constructorDeclaration.Parent is TypeDeclaration { PrimaryConstructorParameters: var parameters })
{ {
foreach (var (cpd, ppd) in constructorDeclaration.Parameters.Zip(parameters)) foreach (var (cpd, ppd) in constructorDeclaration.Parameters.Zip(parameters))

Loading…
Cancel
Save