当前位置: 首页>>代码示例>>C#>>正文


C# DataContext.PushCodeBlock方法代码示例

本文整理汇总了C#中DataContext.PushCodeBlock方法的典型用法代码示例。如果您正苦于以下问题:C# DataContext.PushCodeBlock方法的具体用法?C# DataContext.PushCodeBlock怎么用?C# DataContext.PushCodeBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataContext的用法示例。


在下文中一共展示了DataContext.PushCodeBlock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateFunction

        private static void GenerateFunction(FunctionNode node, DataContext data)
        {
            Append(data, "new ScriptFunction(new Func<ScriptExecutionContext, dynamic[]");

            if (node.Args.Count > 0)
            {
                Append(data, ", ");
                Append(data, string.Join(", ", Enumerable.Repeat("dynamic", node.Args.Count)));
            }

            string functionName = "Function" + data.GetNextFunctionIndex();
            Append(data, ", dynamic>({0})", functionName);

            data.VariableMap.PushFrame();

            Append(data, ", ");
            if (node.Args.Count > 0)
            {
                Append(data, "new dynamic[] {{ ");

                for (int i = 0; i < node.Args.Count; i++)
                {
                    data.VariableMap.DeclareVariable(node.Args[i].VariableName, node.Line, node.Column);

                    if (i != 0)
                        Append(data, ", ");

                    if (node.Args[i].ValueExpression != null)
                    {
                        GenerateExpression(node.Args[i].ValueExpression, data);
                    }
                    else
                    {
                        Append(data, "null");
                    }
                }

                Append(data, " }}");
            }
            else
            {
                Append(data, "null");
            }

            var variableDeclarations = node.BodyStatementBlock.Find<VariableDeclarationNode>().Where(x => x.FindParent<FunctionNode>() == node).Select(x => x.VariableName);

            var capturedVariables = node.BodyStatementBlock.Find<VariableReferenceNode>()
                .Where(x =>
                    x.FindParent<FunctionNode>() == node &&
                    !variableDeclarations.Contains(x.VariableName) &&
                    !data.VariableMap.IsVariableDeclaredInCurrentFrame(x.VariableName)
                )
                .ToArray();

            Append(data, ", ");
            if (capturedVariables.Length > 0)
            {
                Append(data, "new dynamic[] {{ ");

                for (int i = 0; i < capturedVariables.Length; i++)
                {
                    if (i != 0)
                        Append(data, ", ");

                    Append(data, "context[\"{0}\"]", capturedVariables[i].VariableName);
                }

                Append(data, " }}");
            }
            else
            {
                Append(data, "null");
            }

            Append(data, ")");

            data.PushCodeBlock(functionName);
            data.CodeBlock.Padding = 2;

            string returnType = "dynamic";

            if (node.IsGenerator)
                returnType = "IEnumerable<dynamic>";

            Append(data, "private static {0} {1}(ScriptExecutionContext context, dynamic[] captures", returnType, functionName);

            if (node.Args.Count > 0)
            {
                Append(data, ", ");
                Append(data, string.Join(", ", node.Args.Select(x => "dynamic " + data.VariableMap.GetVariableName(x.VariableName))));
            }

            EndLine(data, ")");

            WriteLine(data, "{{");

            data.CodeBlock.Padding++;

            if (!node.IsGenerator)
                WriteLine(data, "context.PushStackFrame(\"{0}\");", !node.IsAnonymous ? node.FunctionName : "<anonymous>");
//.........这里部分代码省略.........
开发者ID:smack0007,项目名称:Snowflake,代码行数:101,代码来源:CodeGenerator.cs


注:本文中的DataContext.PushCodeBlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。