次の方法で共有


CompilerParameters クラス

コンパイラを呼び出すために使用するパラメータを表します。

名前空間: System.CodeDom.Compiler
アセンブリ: System (system.dll 内)

構文

'宣言
<SerializableAttribute> _
Public Class CompilerParameters
'使用
Dim instance As CompilerParameters
[SerializableAttribute] 
public class CompilerParameters
[SerializableAttribute] 
public ref class CompilerParameters
/** @attribute SerializableAttribute() */ 
public class CompilerParameters
SerializableAttribute 
public class CompilerParameters

解説

CompilerParameters オブジェクトは、ICodeCompiler インターフェイスの設定とオプションを表します。

実行可能プログラムをコンパイルする場合は、GenerateExecutable プロパティを true に設定する必要があります。GenerateExecutablefalse に設定すると、コンパイラはクラス ライブラリを生成します。既定では、新しい CompilerParameters は、その GenerateExecutable プロパティを false に設定して初期化されます。CodeDOM グラフから実行可能ファイルをコンパイルする場合は、CodeEntryPointMethod がグラフで定義されている必要があります。コードのエントリ ポイントが複数ある場合は、エントリ ポイントを定義したクラスのうち、使用するクラスの名前を MainClass プロパティに設定して示すことができます。

出力アセンブリのファイル名は、OutputAssembly プロパティに指定できます。この指定を行わない場合は、既定の出力ファイル名が使用されます。生成されるアセンブリにデバッグ情報を含めるには、IncludeDebugInformation プロパティを true に設定します。プロジェクトで参照しているアセンブリがある場合は、コンパイルを実行するときに使用される CompilerParametersReferencedAssemblies プロパティに設定される StringCollection の項目として、そのアセンブリ名を指定する必要があります。

GenerateInMemory プロパティを true に設定することで、ディスクではなくメモリに書き込まれるアセンブリをコンパイルできます。アセンブリをメモリ上に生成すると、コードでは、生成されたアセンブリへの参照を CompilerResultsCompiledAssembly プロパティから取得できます。アセンブリがディスクに書き込まれる場合は、生成されたアセンブリへのパスを CompilerResultsPathToAssembly プロパティから取得できます。

コンパイルを中断する警告レベルを指定するには、WarningLevel プロパティに、コンパイルを中断する警告レベルを表す整数を設定します。TreatWarningsAsErrors プロパティを true に設定して、警告が発生した場合にコンパイルを中断するようにコンパイラを構成することもできます。

コンパイル処理を実行するときに使用するカスタム コマンドライン引数の文字列を指定するには、CompilerOptions プロパティに文字列を設定します。コンパイラ プロセスを起動するために Win32 セキュリティ トークンが必要な場合は、そのトークンを UserToken プロパティに指定します。コンパイルされるアセンブリに .NET Framework リソース ファイルを含めるには、リソース ファイルの名前を EmbeddedResources プロパティに追加します。別のアセンブリ内の .NET Framework リソースを参照するには、リソース ファイルの名前を LinkedResources プロパティに追加します。コンパイルされるアセンブリに Win32 リソース ファイルを含めるには、Win32 リソース ファイルの名前を Win32Resource プロパティに指定します。

注意

このクラスは、すべてのメンバに適用されるリンク確認要求および継承確認要求をクラス レベルで格納します。直前の呼び出し元または派生クラスに完全信頼のアクセス許可がない場合、SecurityException がスローされます。セキュリティ要求の詳細については、「リンク確認要求」および「継承確認要求」を参照してください。

使用例

CompilerParametersを使用して、コンパイラの各種設定とオプションを指定する例を次に示します。

Public Shared Function CompileCode(provider As CodeDomProvider, _
    sourceFile As String, exeFile As String) As Boolean
   
    Dim cp As 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) Then
        ' Specify the class that contains 
        ' the main method of the executable.
        cp.MainClass = "Samples.Class1"
    End If
      
    If provider.Supports(GeneratorSupport.Resources) Then
        ' Set the embedded resource file of the assembly.
        ' This is useful for culture-neutral resources, 
        ' or default (fallback) resources.
        cp.EmbeddedResources.Add("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("nb-no.resources")
    End If
      
    ' Invoke compilation.
    Dim cr As CompilerResults = _
        provider.CompileAssemblyFromFile(cp, sourceFile)
      
    If cr.Errors.Count > 0 Then
        ' Display compilation errors.
        Console.WriteLine("Errors building {0} into {1}", _
            sourceFile, cr.PathToAssembly)
        Dim ce As CompilerError
        For Each ce In  cr.Errors
            Console.WriteLine("  {0}", ce.ToString())
            Console.WriteLine()
        Next ce
    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())
    End If
      
    ' Return the results of compilation.
    If cr.Errors.Count > 0 Then
        Return False
    Else
        Return True
    End If
End Function '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 (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("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("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;
    }
}
static bool CompileCode( CodeDomProvider^ provider,
   String^ sourceFile,
   String^ exeFile )
{

   CompilerParameters^ cp = gcnew CompilerParameters;
   if ( !cp)  
   {
      return false;
   }

   // 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 = gcnew TempFileCollection( ".",true );

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

   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( "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( "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 );
      for each ( CompilerError^ ce in cr->Errors )
      {
         Console::WriteLine( "  {0}", ce->ToString() );
         Console::WriteLine();
      }
   }
   else
   {
      Console::WriteLine( "Source {0} built into {1} successfully.",
         sourceFile, cr->PathToAssembly );
   }

   // Return the results of compilation.
   if ( cr->Errors->Count > 0 )
   {
      return false;
   }
   else
   {
      return true;
   }
}

.NET Framework のセキュリティ

継承階層

System.Object
  System.CodeDom.Compiler.CompilerParameters

スレッド セーフ

この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。

プラットフォーム

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

参照

関連項目

CompilerParameters メンバ
System.CodeDom.Compiler 名前空間