public static BlockStatement CreateNotImplementedBlock()
        {
            BlockStatement b = new BlockStatement();

            b.Throw(new TypeReference("NotImplementedException").New());
            return(b);
        }
Exemplo n.º 2
0
        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));
        }