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


C# CompilerParameters.MainClass属性代码示例

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


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

示例1: CompileCode

public static bool CompileCode(CodeDomProvider provider, 
          String sourceFile, 
          String exeFile)
      {

          CompilerParameters cp = new CompilerParameters();

          // Generate an executable instead of 
          // a class library.
          cp.GenerateExecutable = true;

          // Set the assembly file name to generate.
          cp.OutputAssembly = exeFile;

          // Generate debug information.
          cp.IncludeDebugInformation = true;

          // Add an assembly reference.
          cp.ReferencedAssemblies.Add( "System.dll" );

          // Save the assembly as a physical file.
          cp.GenerateInMemory = false;

          // Set the level at which the compiler 
          // should start displaying warnings.
          cp.WarningLevel = 3;

          // Set whether to treat all warnings as errors.
          cp.TreatWarningsAsErrors = false;
          
          // Set compiler argument to optimize output.
          cp.CompilerOptions = "/optimize";

          // Set a temporary files collection.
          // The TempFileCollection stores the temporary files
          // generated during a build in the current directory,
          // and does not delete them after compilation.
          cp.TempFiles = new TempFileCollection(".", true);

          if (provider.Supports(GeneratorSupport.EntryPointMethod))
          {
              // Specify the class that contains 
              // the main method of the executable.
              cp.MainClass = "Samples.Class1";
          }

          if (Directory.Exists("Resources"))
          {
              if (provider.Supports(GeneratorSupport.Resources))
              {
                  // Set the embedded resource file of the assembly.
                  // This is useful for culture-neutral resources,
                  // or default (fallback) resources.
                  cp.EmbeddedResources.Add("Resources\\Default.resources");

                  // Set the linked resource reference files of the assembly.
                  // These resources are included in separate assembly files,
                  // typically localized for a specific language and culture.
                  cp.LinkedResources.Add("Resources\\nb-no.resources");
              }
          }

          // Invoke compilation.
          CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile);

          if(cr.Errors.Count > 0)
          {
              // Display compilation errors.
              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.ToString());
          }
        
          // Return the results of compilation.
          if (cr.Errors.Count > 0)
          {
              return false;
          }
          else 
          {
              return true;
          }
      }
开发者ID:.NET开发者,项目名称:System.CodeDom.Compiler,代码行数:94,代码来源:CompilerParameters.MainClass


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