private void CorrectStatements(string name, CodeStatementCollection stmts, CodeTypeReference returnType) { _name = name; _referenceCount = 0; new CodeDomWalker(stmts).Walk(RenameReturnValue); _name = null; CodeObjectSource source = Utils.GetTypeReferenceSource(returnType); Type type = (source == null ? null : source.Target as Type); CodeExpression defaultExpr = (type != null && type.IsPrimitive ? (CodeExpression) new CodeDefaultValueExpression(returnType) : new CodePrimitiveExpression(null)); CodeExpression returnExpr = null; if (_referenceCount > 0) { bool requiresDeclaration = true; CodeAssignStatement assignStmt = stmts[stmts.Count - 1] as CodeAssignStatement; if (assignStmt != null) { CodeVariableReferenceExpression variableStmt = assignStmt.Left as CodeVariableReferenceExpression; if (variableStmt != null && StringUtils.CaseInsensitiveEquals(variableStmt.VariableName, _returnVariableName)) { stmts.RemoveAt(stmts.Count - 1); returnExpr = assignStmt.Right; requiresDeclaration = (_referenceCount > 1); } } if (requiresDeclaration) { CodeExpression initExpression = null; assignStmt = stmts[0] as CodeAssignStatement; if (assignStmt != null) { CodeVariableReferenceExpression variableStmt = assignStmt.Left as CodeVariableReferenceExpression; if (variableStmt != null && StringUtils.CaseInsensitiveEquals(variableStmt.VariableName, _returnVariableName)) { stmts.RemoveAt(0); initExpression = assignStmt.Right; } } if (initExpression == null) { initExpression = defaultExpr; } stmts.Insert( 0, new CodeVariableDeclarationStatement( returnType, _returnVariableName, initExpression)); } if (returnExpr == null) { returnExpr = new CodeVariableReferenceExpression(_returnVariableName); } } else { returnExpr = defaultExpr; } stmts.Add(new CodeMethodReturnStatement(returnExpr)); }
private CodeStatement ExtractCtorStatement (CodeStatementCollection statements) { CodeStatement result = null; CodeAssignStatement assignment = null; int toRemove = -1; for (int i=0; i < statements.Count; i++) { assignment = statements[i] as CodeAssignStatement; if (assignment != null && assignment.Right is CodeObjectCreateExpression) { result = assignment; toRemove = i; } } if (toRemove != -1) statements.RemoveAt (toRemove); return result; }
// Used to remove the ctor from the statement colletion in order for the ctor statement to be moved. // private CodeStatement ExtractCtorStatement (IDesignerSerializationManager manager, CodeStatementCollection statements, object component) { CodeStatement result = null; CodeAssignStatement assignment = null; CodeObjectCreateExpression ctor = null; int toRemove = -1; for (int i=0; i < statements.Count; i++) { assignment = statements[i] as CodeAssignStatement; if (assignment != null) { ctor = assignment.Right as CodeObjectCreateExpression; if (ctor != null && manager.GetType (ctor.CreateType.BaseType) == component.GetType ()) { result = assignment; toRemove = i; } } } if (toRemove != -1) statements.RemoveAt (toRemove); return result; }