📄 codedomvisitor.cs
字号:
for (int j = 1; j < typeRef.RankSpecifier[i]; ++j) {
builder.Append(',');
}
builder.Append(']');
}
return builder.ToString();
}
public override object Visit(LocalVariableDeclaration localVariableDeclaration, object data)
{
CodeVariableDeclarationStatement declStmt = null;
CodeTypeReference type = new CodeTypeReference(Convert(localVariableDeclaration.Type));
foreach (VariableDeclaration var in localVariableDeclaration.Variables) {
if (var.Initializer != null) {
declStmt = new CodeVariableDeclarationStatement(type,
var.Name,
(CodeExpression)((INode)var.Initializer).AcceptVisitor(this, data));
} else {
declStmt = new CodeVariableDeclarationStatement(type,
var.Name);
}
}
AddStmt(declStmt);
return declStmt;
}
public override object Visit(EmptyStatement emptyStatement, object data)
{
CodeSnippetStatement emptyStmt = new CodeSnippetStatement();
AddStmt(emptyStmt);
return emptyStmt;
}
public override object Visit(ReturnStatement returnStatement, object data)
{
ProcessSpecials(returnStatement.Specials);
CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement((CodeExpression)returnStatement.ReturnExpression.AcceptVisitor(this,data));
AddStmt(returnStmt);
return returnStmt;
}
public override object Visit(IfStatement ifStatement, object data)
{
ProcessSpecials(ifStatement.Specials);
CodeConditionStatement ifStmt = new CodeConditionStatement();
ifStmt.Condition = (CodeExpression)ifStatement.Condition.AcceptVisitor(this, data);
codeStack.Push(ifStmt.TrueStatements);
ifStatement.EmbeddedStatement.AcceptChildren(this, data);
codeStack.Pop();
AddStmt(ifStmt);
return ifStmt;
}
public override object Visit(IfElseStatement ifElseStatement, object data)
{
ProcessSpecials(ifElseStatement.Specials);
CodeConditionStatement ifStmt = new CodeConditionStatement();
ifStmt.Condition = (CodeExpression)ifElseStatement.Condition.AcceptVisitor(this, data);
codeStack.Push(ifStmt.TrueStatements);
ifElseStatement.EmbeddedStatement.AcceptChildren(this, data);
codeStack.Pop();
codeStack.Push(ifStmt.FalseStatements);
ifElseStatement.EmbeddedElseStatement.AcceptChildren(this, data);
codeStack.Pop();
AddStmt(ifStmt);
return ifStmt;
}
public override object Visit(WhileStatement whileStatement, object data)
{
return null;
}
public override object Visit(DoWhileStatement doWhileStatement, object data)
{
return null;
}
public override object Visit(ForStatement forStatement, object data)
{
CodeIterationStatement forLoop = new CodeIterationStatement();
if (forStatement.Initializers != null)
{
if (forStatement.Initializers.Count > 1)
{
throw new NotSupportedException("CodeDom does not support Multiple For-Loop Initializer Statements");
}
foreach (object o in forStatement.Initializers)
{
if (o is Expression)
{
forLoop.InitStatement = new CodeExpressionStatement((CodeExpression)((Expression)o).AcceptVisitor(this,data));
}
if (o is Statement)
{
codeStack.Push(NullStmtCollection);
forLoop.InitStatement = (CodeStatement)((Statement)o).AcceptVisitor(this, data);
codeStack.Pop();
}
}
}
if (forStatement.Condition == null)
{
forLoop.TestExpression = new CodePrimitiveExpression(true);
}
else
{
forLoop.TestExpression = (CodeExpression)forStatement.Condition.AcceptVisitor(this, data);
}
codeStack.Push(forLoop.Statements);
forStatement.EmbeddedStatement.AcceptVisitor(this, data);
codeStack.Pop();
if (forStatement.Iterator != null)
{
if (forStatement.Initializers.Count > 1)
{
throw new NotSupportedException("CodeDom does not support Multiple For-Loop Iterator Statements");
}
foreach (Statement stmt in forStatement.Iterator)
{
forLoop.IncrementStatement = (CodeStatement)stmt.AcceptVisitor(this, data);
}
}
AddStmt(forLoop);
return forLoop;
}
public override object Visit(LabelStatement labelStatement, object data)
{
ProcessSpecials(labelStatement.Specials);
System.CodeDom.CodeLabeledStatement labelStmt = new CodeLabeledStatement(labelStatement.Label,(CodeStatement)labelStatement.AcceptVisitor(this, data));
// Add Statement to Current Statement Collection
AddStmt(labelStmt);
return labelStmt;
}
public override object Visit(GotoStatement gotoStatement, object data)
{
ProcessSpecials(gotoStatement.Specials);
System.CodeDom.CodeGotoStatement gotoStmt = new CodeGotoStatement(gotoStatement.Label);
// Add Statement to Current Statement Collection
AddStmt(gotoStmt);
return gotoStmt;
}
public override object Visit(SwitchStatement switchStatement, object data)
{
throw new NotSupportedException("CodeDom does not support Switch Statement");
}
public override object Visit(BreakStatement breakStatement, object data)
{
return null;
}
public override object Visit(ContinueStatement continueStatement, object data)
{
return null;
}
public override object Visit(GotoCaseStatement gotoCaseStatement, object data)
{
return null;
}
public override object Visit(ForeachStatement foreachStatement, object data)
{
return null;
}
public override object Visit(LockStatement lockStatement, object data)
{
return null;
}
public override object Visit(UsingStatement usingStatement, object data)
{
return null;
}
public override object Visit(TryCatchStatement tryCatchStatement, object data)
{
ProcessSpecials(tryCatchStatement.Specials);
// add a try-catch-finally
CodeTryCatchFinallyStatement tryStmt = new CodeTryCatchFinallyStatement();
codeStack.Push(tryStmt.TryStatements);
ProcessSpecials(tryCatchStatement.StatementBlock.Specials);
tryCatchStatement.StatementBlock.AcceptChildren(this, data);
codeStack.Pop();
if (tryCatchStatement.FinallyBlock != null)
{
codeStack.Push(tryStmt.FinallyStatements);
ProcessSpecials(tryCatchStatement.FinallyBlock.Specials);
tryCatchStatement.FinallyBlock.AcceptChildren(this,data);
codeStack.Pop();
}
if (tryCatchStatement.CatchClauses != null)
{
foreach (CatchClause clause in tryCatchStatement.CatchClauses)
{
CodeCatchClause catchClause = new CodeCatchClause(clause.VariableName);
catchClause.CatchExceptionType = new CodeTypeReference(clause.Type);
tryStmt.CatchClauses.Add(catchClause);
codeStack.Push(catchClause.Statements);
ProcessSpecials(clause.StatementBlock.Specials);
clause.StatementBlock.AcceptChildren(this, data);
codeStack.Pop();
}
}
// Add Statement to Current Statement Collection
AddStmt(tryStmt);
return tryStmt;
}
public override object Visit(ThrowStatement throwStatement, object data)
{
ProcessSpecials(throwStatement.Specials);
CodeThrowExceptionStatement throwStmt = new CodeThrowExceptionStatement((CodeExpression)throwStatement.ThrowExpression.AcceptVisitor(this, data));
// Add Statement to Current Statement Collection
AddStmt(throwStmt);
return throwStmt;
}
public override object Visit(FixedStatement fixedStatement, object data)
{
throw new NotSupportedException("CodeDom does not support Fixed Statement");
}
public override object Visit(PrimitiveExpression expression, object data)
{
return new CodePrimitiveExpression(expression.Value);
}
public override object Visit(BinaryOperatorExpression expression, object data)
{
CodeBinaryOperatorType op = CodeBinaryOperatorType.Add;
switch (expression.Op) {
case BinaryOperatorType.Add:
op = CodeBinaryOperatorType.Add;
break;
case BinaryOperatorType.BitwiseAnd:
op = CodeBinaryOperatorType.BitwiseAnd;
break;
case BinaryOperatorType.BitwiseOr:
op = CodeBinaryOperatorType.BitwiseOr;
break;
case BinaryOperatorType.LogicalAnd:
op = CodeBinaryOperatorType.BooleanAnd;
break;
case BinaryOperatorType.LogicalOr:
op = CodeBinaryOperatorType.BooleanOr;
break;
case BinaryOperatorType.Divide:
op = CodeBinaryOperatorType.Divide;
break;
case BinaryOperatorType.GreaterThan:
op = CodeBinaryOperatorType.GreaterThan;
break;
case BinaryOperatorType.GreaterThanOrEqual:
op = CodeBinaryOperatorType.GreaterThanOrEqual;
break;
case BinaryOperatorType.Equality:
op = CodeBinaryOperatorType.IdentityEquality;
break;
case BinaryOperatorType.InEquality:
op = CodeBinaryOperatorType.IdentityInequality;
break;
case BinaryOperatorType.LessThan:
op = CodeBinaryOperatorType.LessThan;
break;
case BinaryOperatorType.LessThanOrEqual:
op = CodeBinaryOperatorType.LessThanOrEqual;
break;
case BinaryOperatorType.Modulus:
op = CodeBinaryOperatorType.Modulus;
break;
case BinaryOperatorType.Multiply:
op = CodeBinaryOperatorType.Multiply;
break;
case BinaryOperatorType.Subtract:
op = CodeBinaryOperatorType.Subtract;
break;
case BinaryOperatorType.ValueEquality:
op = CodeBinaryOperatorType.ValueEquality;
break;
case BinaryOperatorType.ShiftLeft:
// CodeDOM suxx
op = CodeBinaryOperatorType.Multiply;
break;
case BinaryOperatorType.ShiftRight:
// CodeDOM suxx
op = CodeBinaryOperatorType.Multiply;
break;
case BinaryOperatorType.IS:
op = CodeBinaryOperatorType.IdentityEquality;
break;
case BinaryOperatorType.AS:
op = CodeBinaryOperatorType.IdentityEquality;
break;
case BinaryOperatorType.ExclusiveOr:
// CodeDOM suxx
op = CodeBinaryOperatorType.BitwiseAnd;
break;
}
return new CodeBinaryOperatorExpression((CodeExpression)expression.Left.AcceptVisitor(this, data),
op,
(CodeExpression)expression.Right.AcceptVisitor(this, data));
}
public override object Visit(ParenthesizedExpression expression, object data)
{
return expression.Expression.AcceptVisitor(this, data);
}
public override object Visit(InvocationExpression invocationExpression, object data)
{
Expression target = invocationExpression.TargetObject;
CodeExpression targetExpr;
string methodName = null;
if (target == null) {
targetExpr = new CodeThisReferenceExpression();
} else if (target is FieldReferenceExpression) {
FieldReferenceExpression fRef = (FieldReferenceExpression)target;
targetExpr = (CodeExpression)fRef.TargetObject.AcceptVisitor(this, data);
if (fRef.TargetObject is FieldReferenceExpression) {
FieldReferenceExpression fRef2 = (FieldReferenceExpression)fRef.TargetObject;
if (fRef2.FieldName != null && Char.IsUpper(fRef2.FieldName[0])) {
// an exception is thrown if it doesn't end in an indentifier exception
// for example for : this.MyObject.MyMethod() leads to an exception, which
// is correct in this case ... I know this is really HACKY :)
try {
CodeExpression tExpr = ConvertToIdentifier(fRef2);
if (tExpr != null) {
targetExpr = tExpr;
}
} catch (Exception) {}
}
}
methodName = fRef.FieldName;
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -