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


C# VBCodeProvider.GenerateCodeFromCompileUnit方法代码示例

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


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

示例1: Compile

        internal static TemplateCompilationResult Compile(
            Type templateType,
            string templateBody,
            IEnumerable<string> assemblyFileNames,
            IEnumerable<string> namespaces,
            string tempDirectory)
        {
            LoadRuntimeBinder();

            string className;
            var compileUnit = GetCodeCompileUnit(templateType, namespaces, templateBody, out className);

            string sourceCode;
            CodeDomProvider codeProvider;
            switch (Language)
            {
             case TemplateCompilationLanguage.CSharp:
                    codeProvider = new CSharpCodeProvider();
                    break;
                case TemplateCompilationLanguage.VisualBasic:
                    codeProvider = new VBCodeProvider();
                    break;
                default:
                    throw new NotSupportedException("Language not supported.");
            }
            var builder = new StringBuilder();

            using (var writer = new StringWriter(builder, CultureInfo.InvariantCulture))
            {
                codeProvider.GenerateCodeFromCompileUnit(compileUnit, writer, new CodeGeneratorOptions());
                sourceCode = builder.ToString();
            }

            var parameters = CreateCompilerParameters(tempDirectory, assemblyFileNames);
            var compileResult = codeProvider.CompileAssemblyFromDom(parameters, compileUnit);
            if (compileResult.Errors != null && compileResult.Errors.Count > 0)
                throw new TemplateCompilationException(compileResult.Errors, sourceCode, templateBody);

            var fullClassName = TEMPLATES_NAMESPACE + "." + className;

            return new TemplateCompilationResult
            {
                Type = compileResult.CompiledAssembly.GetType(fullClassName),
                SourceCode = sourceCode
            };
        }
开发者ID:volkovku,项目名称:RazorTemplates,代码行数:46,代码来源:TemplateCompiler.cs

示例2: GenerateVBTo

 /// <summary>
 /// Generates VB text from a compile unit and writes it to a text writer with the given options.
 /// </summary>
 /// <param name="compileUnit">The compile unit to generate text from.</param>
 /// <param name="writer">The text writer to write to.</param>
 /// <param name="options">The generation options.</param>
 public static void GenerateVBTo(this CodeCompileUnit compileUnit, TextWriter writer, CodeGeneratorOptions options)
 {
     using (var provider = new VBCodeProvider())
     {
         provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
     }
 }
开发者ID:DzmitrySo,项目名称:mc,代码行数:13,代码来源:CodeDomGenerator.cs

示例3: Main


//.........这里部分代码省略.........
                            CurrentlyExecutingAssemblyClass currentlyExecutingAssemblyClass=new CurrentlyExecutingAssemblyClass(""Hello World"");
                            Console.WriteLine(currentlyExecutingAssemblyClass.CurrentlyExecutingProperty);
                        "
            };
            codeMemberMethod.Statements.Add(codeSnippetExpression);
            codeTypeDeclaration.Members.Add(codeMemberMethod);
            #endregion

            #region Create Region for Class
            codeRegionDirectiveStart = new CodeRegionDirective(CodeRegionMode.Start, "Start MyType");
            codeRegionDirectiveEnd = new CodeRegionDirective(CodeRegionMode.End, "Start MyType");
            codeTypeDeclaration.StartDirectives.Add(codeRegionDirectiveStart);
            codeTypeDeclaration.EndDirectives.Add(codeRegionDirectiveEnd);
            #endregion

            #region render code
            string sourceCS = string.Empty;
            StringBuilder stringBuilder = new StringBuilder();
            CodeDomProvider codeDomProvider = null;

            //Bracing style Block->same line C->new line
            //GenerateCodeFromCompileUnit reserves the right to rearrange your code the way it knows best
            CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions()
                {
                    BracingStyle = "C",
                    VerbatimOrder = false,
                    BlankLinesBetweenMembers = true,
                    ElseOnClosing = false
                };
            //in c#
            using (StringWriter stringWriter = new StringWriter())
            {
                codeDomProvider = new CSharpCodeProvider();
                codeDomProvider.GenerateCodeFromCompileUnit(codeCompileUnit,
                                                            stringWriter,
                                                            codeGeneratorOptions);
                sourceCS = stringWriter.ToString();

            }
            //in visualbasic
            string sourceVB = string.Empty;
            using (StringWriter stringWriter = new StringWriter())
            {
                codeDomProvider = new VBCodeProvider();
                codeDomProvider.GenerateCodeFromCompileUnit(codeCompileUnit,
                                                            stringWriter,
                                                            codeGeneratorOptions);
                sourceVB = stringWriter.ToString();
            }

            //output source
            //Console.WriteLine(sourceCS);
            //Console.ReadLine();
            //Console.WriteLine(sourceVB);
            //Console.ReadLine();
            #endregion

            #region declare compiler objects
            CompilerParameters compilerParameters = null;
            CompilerResults compilerResults = null;
            int lineNumber = 0;
            #endregion

            #region compile CS source
            CSharpCodeProvider csharpCodeProvider = null;
开发者ID:flipthetrain,项目名称:KCDC2015_CODEDOM,代码行数:66,代码来源:Program.cs


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