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


C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromStatement方法代码示例

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


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

示例1: GenerateCode

 public void GenerateCode(CodeDomGenerator.Language language)
 {
     switch (language)
     {
         case CodeDomGenerator.Language.CSharp:
             if (_csStatement != null)
                 using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                 {
                     System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                     using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                     {
                         provider.GenerateCodeFromStatement(_csStatement, tw, opts);
                         Value = tw.InnerWriter.ToString();
                     }
                 }
             break;
         case CodeDomGenerator.Language.VB:
             if (_vbStatement != null)
                 using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                 {
                     System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                     using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                     {
                         provider.GenerateCodeFromStatement(_vbStatement, tw, opts);
                         Value = tw.InnerWriter.ToString();
                     }
                 }
             break;
         default:
             throw new NotImplementedException(language.ToString());
     }
 }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:32,代码来源:CodeSnippetStatement.cs

示例2: GenerateCode

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.WriteLine("do");
                            tw.WriteLine("{");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.Write("} while (");
                            if (TestExpression != null)
                                provider.GenerateCodeFromExpression(TestExpression, tw, opts);
                            tw.WriteLine(");");

                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.WriteLine("Do");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.Write("Loop Until ");
                            if (TestExpression != null)
                                provider.GenerateCodeFromExpression(TestExpression, tw, opts);
                            tw.WriteLine();

                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:56,代码来源:CodeWhileStatement.cs

示例3: GenerateCode

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.Write("foreach (");
                            provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts);
                            tw.Write(" {0} in ", provider.CreateValidIdentifier(IterationItemName));
                            provider.GenerateCodeFromExpression(IterExpression, tw, opts);
                            tw.WriteLine(")");
                            tw.WriteLine("{");
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.WriteLine("}");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.Write("For Each {0} As ", provider.CreateValidIdentifier(IterationItemName));
                            provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(IterationItemType), tw, opts);
                            tw.Write(" in ");
                            provider.GenerateCodeFromExpression(IterExpression, tw, opts);
                            tw.WriteLine();
                            tw.Indent++;
                            if (Statements != null)
                                foreach (CodeStatement statement in Statements)
                                {
                                    provider.GenerateCodeFromStatement(statement, tw, opts);
                                }
                            tw.Indent--;
                            tw.WriteLine("Next");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:56,代码来源:CodeForeachStatement.cs

示例4: GenerateCode

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {

                            tw.Write("switch (");
                            if (SwitchExpression != null)
                                provider.GenerateCodeFromExpression(SwitchExpression, tw, opts);
                            tw.WriteLine(")");
                            tw.WriteLine("{");
                            tw.Indent++;
                            foreach (CaseBlock c in m_cases)
                            {
                                if (c.Statements != null)
                                {
                                    if (c.CaseExpression != null)
                                    {
                                        tw.Write("case ");
                                        provider.GenerateCodeFromExpression(c.CaseExpression, tw, opts);
                                        tw.WriteLine(":");
                                    }
                                    else
                                    {
                                        tw.WriteLine("default:");
                                    }

                                    tw.Indent++;
                                    foreach (CodeStatement stmt in c.Statements)
                                    {
                                        provider.GenerateCodeFromStatement(stmt, tw, opts);
                                    }
                                    tw.Indent--;
                                }
                                //else
                                //{
                                //    tw.Indent++;
                                //    tw.WriteLine("break;");
                                //    tw.Indent--;
                                //}
                            }
                            tw.Indent--;
                            tw.WriteLine("}");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            tw.Write("Select Case ");
                            if (SwitchExpression != null)
                                provider.GenerateCodeFromExpression(SwitchExpression, tw, opts);
                            tw.WriteLine();
                            tw.Indent++;
                            foreach (CaseBlock c in m_cases)
                            {
                                if (c.Statements != null)
                                {
                                    if (c.CaseExpression != null)
                                    {
                                        tw.Write("Case ");
                                        provider.GenerateCodeFromExpression(c.CaseExpression, tw, opts);
                                        tw.WriteLine();
                                    }
                                    else
                                    {
                                        tw.WriteLine("Case Else");
                                    }

                                    tw.Indent++;
                                    for (int i = 0; i < c.Statements.Length; i++)
                                    {
                                        CodeStatement stmt = c.Statements[i];
                                        if (!(stmt is CodeExitSelectStatement) || i < c.Statements.Length-1)
                                            provider.GenerateCodeFromStatement(stmt, tw, opts);
                                    }
                                    tw.Indent--;
                                }
                                //else if (i < m_cases.Count - 1)
                                //{
                                //    tw.Indent++;
                                //    tw.WriteLine("Exit Select");
                                //    tw.Indent--;
                                //}
                            }
                            tw.Indent--;
                            tw.WriteLine("End Select");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
//.........这里部分代码省略.........
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:101,代码来源:CodeSwitchStatement.cs

示例5: GenerateCode

        public void GenerateCode(CodeDomGenerator.Language language)
        {
            switch (language)
            {
                case CodeDomGenerator.Language.CSharp:
                    using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            if (_params.Count != 1 || _params[0].Type != null)
                                tw.Write("(");

                            for (int i = 0; i < _params.Count; i++)
                            {
                                if (i > 0)
                                    tw.Write(", ");

                                if (_params[i].Type != null)
                                {
                                    provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(_params[i].Type), tw, opts);
                                    tw.Write(" ");
                                }

                                tw.Write(_params[i].Name);
                            }

                            if (_params.Count != 1 || _params[0].Type != null)
                                tw.Write(")");

                            tw.WriteLine(" => ");
                            tw.WriteLine("{");
                            tw.Indent++;
                            foreach (CodeStatement statement in Statements)
                            {
                                provider.GenerateCodeFromStatement(statement, tw, opts);
                            }
                            tw.Indent--;
                            tw.Write("}");
                            Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                case CodeDomGenerator.Language.VB:
                    using (Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider())
                    {
                        System.CodeDom.Compiler.CodeGeneratorOptions opts = new System.CodeDom.Compiler.CodeGeneratorOptions();
                        using (System.CodeDom.Compiler.IndentedTextWriter tw = new System.CodeDom.Compiler.IndentedTextWriter(new StringWriter(), opts.IndentString))
                        {
                            throw new NotImplementedException();
                            //tw.Write("Function(");

                            //for (int i = 0; i < _params.Count; i++)
                            //{
                            //    if (i > 0)
                            //        tw.Write(", ");

                            //    tw.Write(_params[i].Name);

                            //    if (_params[i].Type != null)
                            //    {
                            //        tw.Write(" As ");
                            //        provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(_params[i].Type), tw, opts);
                            //    }
                            //}

                            //tw.Write(") ");
                            //provider.GenerateCodeFromExpression(Statements, tw, opts);
                            //Value = tw.InnerWriter.ToString();
                        }
                    }
                    break;
                default:
                    throw new NotImplementedException(language.ToString());
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:76,代码来源:CodeLambdaStatements.cs


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