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


C# CSharpCodeProvider.Supports方法代码示例

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


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

示例1: CompileCode

        private static bool CompileCode(List<string> sourceFile, String exeFile, string[] libraries)
        {
            CodeDomProvider provider = new CSharpCodeProvider();
            CompilerParameters cp = new CompilerParameters();
            List<string> codeLines = new List<string>();
            cp.GenerateExecutable = true;
            cp.OutputAssembly = exeFile;
            cp.IncludeDebugInformation = false;

            cp.ReferencedAssemblies.Add("System.dll");
            cp.ReferencedAssemblies.Add("System.core.dll");

            cp.GenerateInMemory = false;
            cp.WarningLevel = 1;
            cp.TreatWarningsAsErrors = false;
            cp.CompilerOptions = "/optimize";

            cp.TempFiles = new TempFileCollection(".", false);

            if (provider.Supports(GeneratorSupport.EntryPointMethod))
            {
                cp.MainClass = "ProblemNamespace.ProblemClass";
            }
            if (libraries != null)
                codeLines.AddRange(libraries.Select(l => "using " + l + ";"));
            codeLines.AddRange(new[]
            {
                "using System;\n",
                "namespace ProblemNamespace\n",
                "{\n",
                "class ProblemClass\n",
                "{\n",
                "static void Main(string[] args)\n",
                "{\n"
            });
            codeLines.AddRange(sourceFile);
            codeLines.AddRange(new[]
            {
                //"Console.ReadKey();\n",
                "}\n",
                "}\n",
                "}\n"
            });

            string finalCode = String.Join("", codeLines);
            CompilerResults cr = provider.CompileAssemblyFromSource(cp, finalCode);

            if (cr.Errors.Count > 0)
            {
                Console.WriteLine("Errors building {0} into {1}",
                    sourceFile, cr.PathToAssembly);
                foreach (CompilerError ce in cr.Errors)
                {
                    Console.WriteLine("  {0}", ce.ToString());
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("Source {0} built into {1} successfully.",
                    sourceFile, cr.PathToAssembly);
                Console.WriteLine("{0} temporary files created during the compilation.",
                    cp.TempFiles.Count);
            }

            return cr.Errors.Count <= 0;
        }
开发者ID:Psykepro,项目名称:Canrum,代码行数:67,代码来源:CodeManager.cs


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