當前位置: 首頁>>代碼示例>>C#>>正文


C# GeneratorSupport枚舉代碼示例

本文整理匯總了C#中System.CodeDom.Compiler.GeneratorSupport枚舉的典型用法代碼示例。如果您正苦於以下問題:C# GeneratorSupport枚舉的具體用法?C# GeneratorSupport怎麽用?C# GeneratorSupport使用的例子?那麽, 這裏精選的枚舉代碼示例或許可以為您提供幫助。


在下文中一共展示了GeneratorSupport枚舉的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,代碼來源:GeneratorSupport


注:本文中的System.CodeDom.Compiler.GeneratorSupport枚舉示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。