mirror of https://github.com/icsharpcode/ILSpy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.6 KiB
42 lines
1.6 KiB
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) |
|
// This code is distributed under MIT X11 license (for details please see \doc\license.txt) |
|
|
|
using System; |
|
using System.Linq; |
|
using ICSharpCode.NRefactory.CSharp; |
|
using Mono.Cecil; |
|
|
|
namespace Decompiler.Transforms.Ast |
|
{ |
|
public class ConvertConstructorCallIntoInitializer : DepthFirstAstVisitor<object, object> |
|
{ |
|
public override object VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data) |
|
{ |
|
ExpressionStatement stmt = constructorDeclaration.Body.Statements.FirstOrDefault() as ExpressionStatement; |
|
if (stmt == null) |
|
return null; |
|
InvocationExpression invocation = stmt.Expression as InvocationExpression; |
|
if (invocation == null) |
|
return null; |
|
MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression; |
|
if (mre != null && mre.MemberName == ".ctor") { |
|
ConstructorInitializer ci = new ConstructorInitializer(); |
|
if (mre.Target is ThisReferenceExpression) |
|
ci.ConstructorInitializerType = ConstructorInitializerType.This; |
|
else if (mre.Target is BaseReferenceExpression) |
|
ci.ConstructorInitializerType = ConstructorInitializerType.Base; |
|
else |
|
return null; |
|
// Move arguments from invocation to initializer: |
|
var arguments = invocation.Arguments.ToArray(); |
|
invocation.Arguments = null; |
|
ci.Arguments = arguments; |
|
// Add the initializer: |
|
constructorDeclaration.Initializer = ci.WithAnnotation(invocation.Annotation<MethodReference>()); |
|
// Remove the statement: |
|
stmt.Remove(); |
|
} |
|
return null; |
|
} |
|
} |
|
}
|
|
|