mirror of https://github.com/icsharpcode/ILSpy.git
4 changed files with 46 additions and 3 deletions
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
// 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; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue