本文整理汇总了C#中ICSharpCode.NRefactory.Ast.BlockStatement.AddStatement方法的典型用法代码示例。如果您正苦于以下问题:C# BlockStatement.AddStatement方法的具体用法?C# BlockStatement.AddStatement怎么用?C# BlockStatement.AddStatement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.NRefactory.Ast.BlockStatement
的用法示例。
在下文中一共展示了BlockStatement.AddStatement方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitStaticVariable
INode InitStaticVariable(string initFieldName, string variableName, Expression initializer, TypeDeclaration typeDeclaration)
{
const string helperMethodName = "InitStaticVariableHelper";
if (typeDeclaration != null) {
if (!typeDeclaration.Children.OfType<MethodDeclaration>().Any(m => m.Name == helperMethodName)) {
// add helper method
var helperMethod = new MethodDeclaration {
Name = helperMethodName,
Modifier = Modifiers.Static,
TypeReference = new TypeReference("System.Boolean", true),
Parameters = {
new ParameterDeclarationExpression(new TypeReference("Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag"), "flag")
},
Body = new BlockStatement()
};
BlockStatement trueBlock = new BlockStatement();
BlockStatement elseIfBlock = new BlockStatement();
BlockStatement falseBlock = new BlockStatement();
helperMethod.Body.AddStatement(
new IfElseStatement(ExpressionBuilder.Identifier("flag").Member("State").Operator(BinaryOperatorType.Equality, new PrimitiveExpression(0))) {
TrueStatement = { trueBlock },
ElseIfSections = {
new ElseIfSection(ExpressionBuilder.Identifier("flag").Member("State").Operator(BinaryOperatorType.Equality, new PrimitiveExpression(2)), elseIfBlock)
},
FalseStatement = { falseBlock }
});
trueBlock.Assign(ExpressionBuilder.Identifier("flag").Member("State"), new PrimitiveExpression(2));
trueBlock.Return(new PrimitiveExpression(true));
elseIfBlock.Throw(new TypeReference("Microsoft.VisualBasic.CompilerServices.IncompleteInitialization").New());
falseBlock.Return(new PrimitiveExpression(false));
typeDeclaration.AddChild(helperMethod);
}
}
BlockStatement tryBlock = new BlockStatement();
BlockStatement ifTrueBlock = new BlockStatement();
tryBlock.AddStatement(new IfElseStatement(ExpressionBuilder.Identifier(helperMethodName).Call(ExpressionBuilder.Identifier(initFieldName)), ifTrueBlock));
ifTrueBlock.Assign(ExpressionBuilder.Identifier(variableName), initializer);
BlockStatement finallyBlock = new BlockStatement();
finallyBlock.Assign(ExpressionBuilder.Identifier(initFieldName).Member("State"), new PrimitiveExpression(1));
BlockStatement lockBlock = new BlockStatement();
lockBlock.AddStatement(new TryCatchStatement(tryBlock, null, finallyBlock));
return new LockStatement(ExpressionBuilder.Identifier(initFieldName), lockBlock);
}