Browse Source

Fixed an issue where [RestClientOptions](https://github.com/restsharp/RestSharp/blob/dev/src/RestSharp/Options/RestClientOptions.cs) could not be decompiled correctly after the commit: `Fixed an issue where the logic of moving the initialization statement of class members in the constructor to the class member declaration was incompatible with complex expressions using parameters.`

pull/3598/head
sonyps5201314 2 months ago
parent
commit
8abe38128e
  1. 26
      ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

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

@ -231,7 +231,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (initializer.DescendantsAndSelf.Any(n => n is ThisReferenceExpression || n is BaseReferenceExpression)) if (initializer.DescendantsAndSelf.Any(n => n is ThisReferenceExpression || n is BaseReferenceExpression))
break; break;
bool assignFromParameters = false; bool assignFromCurrentCtorParameters = false;
var v = initializer.Annotation<ILVariableResolveResult>()?.Variable; var v = initializer.Annotation<ILVariableResolveResult>()?.Variable;
if (v == null) if (v == null)
@ -240,12 +240,10 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} }
if (v != null) if (v != null)
{ {
assignFromParameters = v.Kind == IL.VariableKind.Parameter; assignFromCurrentCtorParameters = v.Kind == IL.VariableKind.Parameter;
} }
else else
{ {
v = default(IL.ILVariable); // Used as a placeholder to represent that it comes from the result of a complex expression.
// If we didn't get an ILVariableResolveResult for the whole initializer expression, // If we didn't get an ILVariableResolveResult for the whole initializer expression,
// walk its descendants and try to find any reference that corresponds to a // walk its descendants and try to find any reference that corresponds to a
// constructor parameter. We consider both ILVariable annotations (including // constructor parameter. We consider both ILVariable annotations (including
@ -258,29 +256,23 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
{ {
if (localVar.Kind == IL.VariableKind.Parameter) if (localVar.Kind == IL.VariableKind.Parameter)
{ {
assignFromParameters = true; if (localVar.Function.Method == ctorMethodDef)
break; {
assignFromCurrentCtorParameters = true;
break;
}
} }
} }
// Also check for high-level parameter symbols in case no IL annotation is present
var sym = node.GetSymbol();
if (sym is IParameter)
{
assignFromParameters = true;
break;
}
} }
} }
if (assignFromParameters) if (assignFromCurrentCtorParameters)
{ {
if (!hasPrimaryCtor) if (!hasPrimaryCtor)
{ {
// When there is no primary constructor, assignments from constructor parameters to class members cannot be transferred. // When there is no primary constructor, assignments from constructor parameters to class members cannot be transferred.
break; break;
} }
if (fieldOrPropertyOrEvent is IField f) if (fieldOrPropertyOrEvent is IField f && v != null)
fieldToVariableMap.Add(f, v); fieldToVariableMap.Add(f, v);
} }
else else

Loading…
Cancel
Save