GenerateCodeFromStatement() public method

public GenerateCodeFromStatement ( CodeStatement statement, TextWriter writer, CodeGeneratorOptions options ) : void
statement CodeStatement
writer System.IO.TextWriter
options CodeGeneratorOptions
return void
Esempio n. 1
0
 /// <summary>Called when extension shall process generated code</summary>
 /// <param name="code">The code</param>
 /// <param name="provider">CodeDOM provider (the language)</param>
 /// <version version="1.5.3">Parameter <c>Provider</c> renamed to <c>provider</c></version>
 public void PostProcess(ref string code, CodeDomProvider provider)
 {
     System.IO.StringWriter tw = new System.IO.StringWriter();
     provider.GenerateCodeFromStatement(new CodeCommentStatement(FirtsLineOfAccessor), tw, new System.CodeDom.Compiler.CodeGeneratorOptions());
     string srch = tw.GetStringBuilder().ToString();
     if (srch.EndsWith("\r\n")) srch = srch.Substring(0, srch.Length - 2);
     else if (srch.EndsWith("\r") || srch.EndsWith("\n")) srch = srch.Substring(0, srch.Length - 1);
     tw = new System.IO.StringWriter();
     CodeTypeDeclaration foo = new CodeTypeDeclaration("foo");
     foo.CustomAttributes.Add(NewAttribute);
     provider.GenerateCodeFromType(foo, tw, new System.CodeDom.Compiler.CodeGeneratorOptions());
     string attr = new System.IO.StringReader(tw.GetStringBuilder().ToString()).ReadLine();
     System.IO.StringReader sr = new System.IO.StringReader(code);
     List<String> Lines = new List<string>();
     do {
         string line = sr.ReadLine();
         if (line == null) break;
         if (line.EndsWith(srch))
             Lines[Lines.Count - 1] = attr + "\r\n" + Lines[Lines.Count - 1];
         else
             Lines.Add(line);
     } while (true);
     System.Text.StringBuilder b = new System.Text.StringBuilder();
     foreach (string line in Lines)
         b.AppendLine(line);
     code = b.ToString();
 }
Esempio n. 2
0
 private void AddSpecFlowFooter(CodeDomProvider codeProvider, TextWriter outputWriter)
 {
     codeProvider.GenerateCodeFromStatement(codeDomHelper.GetEnableWarningsPragma(), outputWriter, null);
     codeProvider.GenerateCodeFromStatement(codeDomHelper.GetEndRegionStatement(), outputWriter, null);
 }
Esempio n. 3
0
        private void AddSpecFlowHeader(CodeDomProvider codeProvider, TextWriter outputWriter)
        {
            const string specFlowHeaderTemplate = @"------------------------------------------------------------------------------
 <auto-generated>
     This code was generated by SpecFlow (http://www.specflow.org/).
     SpecFlow Version:{0}
     SpecFlow Generator Version:{1}

     Changes to this file may cause incorrect behavior and will be lost if
     the code is regenerated.
 </auto-generated>
------------------------------------------------------------------------------";

            var headerReader = new StringReader(string.Format(specFlowHeaderTemplate,
                GetCurrentSpecFlowVersion(),
                TestGeneratorFactory.GeneratorVersion
                ));

            string line;
            while ((line = headerReader.ReadLine()) != null)
            {
                codeProvider.GenerateCodeFromStatement(new CodeCommentStatement(line), outputWriter, null);
            }

            codeProvider.GenerateCodeFromStatement(codeDomHelper.GetStartRegionStatement("Designer generated code"), outputWriter, null);
            codeProvider.GenerateCodeFromStatement(codeDomHelper.GetDisableWarningsPragma(), outputWriter, null);
        }
Esempio n. 4
0
 private static void GenrateMeth(CodeGeneratorOptions opts, CodeDomProvider provider, 
     IndentedTextWriter tw, CodeStatementCollection statements, string methName)
 {
     tw.WriteLine(methName);
     tw.WriteLine("{");
     tw.Indent++;
     foreach (CodeStatement statement in statements)
     {
         provider.GenerateCodeFromStatement(statement, tw, opts);
     }
     tw.Indent--;
     tw.WriteLine("}");
 }
Esempio n. 5
0
 /// <summary>生成成员代码块</summary>
 /// <param name="block"></param>
 /// <param name="generatorType"></param>
 /// <param name="lineNumbers"></param>
 /// <param name="provider"></param>
 /// <param name="options"></param>
 /// <param name="firstMemberFound"></param>
 /// <returns></returns>
 private static Boolean GenerateMemberForBlock(Block block, CodeTypeDeclaration generatorType, Boolean lineNumbers, CodeDomProvider provider, CodeGeneratorOptions options, Boolean firstMemberFound)
 {
     CodeSnippetTypeMember member = null;
     if (!firstMemberFound)
     {
         // 发现第一个<#!后,认为是类成员代码的开始,直到下一个<#!作为结束
         if (block.Type == BlockType.Member)
         {
             firstMemberFound = true;
             if (!String.IsNullOrEmpty(block.Text)) member = new CodeSnippetTypeMember(block.Text);
         }
     }
     else
     {
         // 再次遇到<#!,此时,成员代码准备结束
         if (block.Type == BlockType.Member)
         {
             firstMemberFound = false;
             if (!String.IsNullOrEmpty(block.Text)) member = new CodeSnippetTypeMember(block.Text);
         }
         else if (block.Type == BlockType.Text)
         {
             CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Write", new CodeExpression[] { new CodePrimitiveExpression(block.Text) });
             CodeExpressionStatement statement = new CodeExpressionStatement(expression);
             using (StringWriter writer = new StringWriter())
             {
                 provider.GenerateCodeFromStatement(statement, writer, options);
                 member = new CodeSnippetTypeMember(writer.ToString());
             }
         }
         else if (block.Type == BlockType.Expression)
         {
             CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Write", new CodeExpression[] { new CodeArgumentReferenceExpression(block.Text.Trim()) });
             CodeExpressionStatement statement = new CodeExpressionStatement(expression);
             using (StringWriter writer = new StringWriter())
             {
                 provider.GenerateCodeFromStatement(statement, writer, options);
                 member = new CodeSnippetTypeMember(writer.ToString());
             }
         }
         else if (block.Type == BlockType.Statement)
         {
             member = new CodeSnippetTypeMember(block.Text);
         }
     }
     if (member != null)
     {
         if (lineNumbers)
         {
             Boolean flag = String.IsNullOrEmpty(block.Name);
             Int32 lineNumber = (block.StartLine > 0) ? block.StartLine : 1;
             if (flag)
                 generatorType.Members.Add(new CodeSnippetTypeMember("#line " + lineNumber));
             else
                 member.LinePragma = new CodeLinePragma(block.Name, lineNumber);
             generatorType.Members.Add(member);
             if (flag) generatorType.Members.Add(new CodeSnippetTypeMember("#line default"));
         }
         else
             generatorType.Members.Add(member);
     }
     return firstMemberFound;
 }
Esempio n. 6
0
        private void AddSpecFlowHeader(CodeDomProvider codeProvider, TextWriter outputWriter)
        {
            var specFlowHeaderTemplate = @"------------------------------------------------------------------------------
             <auto-generated>
             This code was generated by SpecFlow (http://www.specflow.org/).
             SpecFlow Version:{0}
             Runtime Version:{1}

             Changes to this file may cause incorrect behavior and will be lost if
             the code is regenerated.
             </auto-generated>
            ------------------------------------------------------------------------------";

            var headerReader = new StringReader(string.Format(specFlowHeaderTemplate,
                GetCurrentSpecFlowVersion(),
                Environment.Version
                ));

            string line;
            while ((line = headerReader.ReadLine()) != null)
            {
                codeProvider.GenerateCodeFromStatement(new CodeCommentStatement(line), outputWriter, null);
            }
        }
Esempio n. 7
0
		public static string GenerateErrorString(Exception ex, CodeDomProvider codeProvider, string fileName, string baseNamespace)
		{
			CodeGeneratorOptions gen = new CodeGeneratorOptions();
			gen.BracingStyle = "C";
			gen.IndentString = "\t";
			gen.VerbatimOrder = true;
			gen.BlankLinesBetweenMembers = false;

			CodeCommentStatementCollection comments = new CodeCommentStatementCollection();
			GenerateHeader(comments, fileName, baseNamespace);

			if (ex is CompileException)
			{
				CompileException cx = (CompileException)ex;
				comments.Add(new CodeCommentStatement(string.Format("Error generating shader:{0}{1} (line: {2} col: {3})", Environment.NewLine, cx.Text, cx.Line, cx.Coloumn)));
			}
			else if (ex is CompileExceptionCollection)
			{
				CompileExceptionCollection cxc = (CompileExceptionCollection)ex;
				for (int i = 0; i < cxc.Count; i++)
				{
					CompileException cx = cxc.GetException(i);
					comments.Add(new CodeCommentStatement(string.Format("Error generating shader:{0}{1} (line: {2} col: {3})", Environment.NewLine, cx.Text, cx.Line, cx.Coloumn)));
				}
			}
			else
			{
				comments.Add(new CodeCommentStatement(string.Format("Unhandled exception in XenFX:{0}{1}", Environment.NewLine, ex.ToString())));
			}
			
			StringBuilder sb = new StringBuilder();
			using (TextWriter writer = new StringWriter(sb))
			{
				foreach (CodeCommentStatement comment in comments)
				{
					codeProvider.GenerateCodeFromStatement(comment, writer, gen);
				}
			}
			return sb.ToString();
		}
Esempio n. 8
0
        private static string GenerateCode(CodeObject c, CodeDomProvider provider)
        {
            var sb = new StringBuilder();
            var writer = new StringWriter(sb);
            var options = new CodeGeneratorOptions();

            if (c is CodeStatement)
            {
                provider.GenerateCodeFromStatement((CodeStatement)c, writer, options);
            }
            else if (c is CodeCompileUnit)
            {
                provider.GenerateCodeFromCompileUnit((CodeCompileUnit)c, writer, options);
            }
            else if (c is CodeExpression)
            {
                provider.GenerateCodeFromExpression((CodeExpression)c, writer, options);
            }
            else if (c is CodeTypeMember)
            {
                provider.GenerateCodeFromMember((CodeTypeMember)c, writer, options);
            }
            else if (c is CodeTypeDeclaration)
            {
                provider.GenerateCodeFromType((CodeTypeDeclaration)c, writer, options);
            }
            else if (c is CodeNamespace)
            {
                provider.GenerateCodeFromNamespace((CodeNamespace)c, writer, options);
            }
            else
            {
                throw new ArgumentException($"Tests not set up for unexpected type: {c.GetType()}");
            }

            return sb.ToString();
        }
Esempio n. 9
0
        private static string GenerateCode(CodeObject c, CodeDomProvider provider)
        {
            var sb = new StringBuilder();
            var writer = new StringWriter(sb);
            var options = new CodeGeneratorOptions();

            if (c is CodeStatement)
            {
                provider.GenerateCodeFromStatement((CodeStatement)c, writer, options);
            }
            else if (c is CodeCompileUnit)
            {
                provider.GenerateCodeFromCompileUnit((CodeCompileUnit)c, writer, options);
            }
            else if (c is CodeExpression)
            {
                provider.GenerateCodeFromExpression((CodeExpression)c, writer, options);
            }
            else if (c is CodeTypeMember)
            {
                provider.GenerateCodeFromMember((CodeTypeMember)c, writer, options);
            }
            else if (c is CodeTypeDeclaration)
            {
                provider.GenerateCodeFromType((CodeTypeDeclaration)c, writer, options);
            }
            else if (c is CodeNamespace)
            {
                provider.GenerateCodeFromNamespace((CodeNamespace)c, writer, options);
            }
            else
            {
                Assert.False(true, $"Unknown type: {c.GetType()}");
            }

            return sb.ToString();
        }
 public void GenerateStatemets(TextWriter tw, CodeDomProvider provider, CodeGeneratorOptions opts, CodeStatement[] statements)
 {
     foreach (var statement in statements)
     {
         provider.GenerateCodeFromStatement(statement, tw, opts);
     }
 }