This documentation is archived and is not being maintained.

CompilerInfo Class

Represents the configuration settings of a language provider. This class cannot be inherited.

Namespace:  System.CodeDom.Compiler
Assembly:  System (in System.dll)

'Declaration
<PermissionSetAttribute(SecurityAction.LinkDemand, Name := "FullTrust")> _
Public NotInheritable Class CompilerInfo
'Usage
Dim instance As CompilerInfo

Use the CompilerInfo class to determine whether a CodeDomProvider implementation is configured on the computer, or to examine the configuration and compiler settings for a specific language provider.

The <system.codedom> Element in the machine configuration file contains the language provider and compiler configuration settings. Each configured language provider has a corresponding compiler configuration element. Each element defines the CodeDomProvider implementation type, supported language names, supported file name extensions, and compiler parameters.

The .NET Framework defines the initial compiler settings in the machine configuration file. Developers and compiler vendors can add configuration settings for a new CodeDomProvider implementation.

The CompilerInfo class provides read-only access to these settings in the machine configuration file. Use the GetLanguages, GetExtensions, and CodeDomProviderType members to examine the corresponding configuration attributes for a language provider. Use the CreateDefaultCompilerParameters method to obtain the compiler options and warning level attribute values for a language provider.

For more details on language provider settings in the configuration file, see Compiler and Language Provider Settings Schema.

NoteNote:

This class contains a link demand at the class level that applies to all members. A SecurityException is thrown when the immediate caller does not have full-trust permission. For details about link demands, see Link Demands.

The following code example displays language provider configuration settings. Command-line arguments are used to specify a language, file name extension, or provider type. For the given input, the example determines the corresponding language provider and displays the configured language compiler settings.

' Command-line argument examples: 
'   <exe_name> 
'      - Displays Visual Basic, C#, and JScript compiler settings. 
'   <exe_name> Language CSharp 
'      - Displays the compiler settings for C#. 
'   <exe_name> All 
'      - Displays settings for all configured compilers. 
'   <exe_name> Config Pascal 
'      - Displays settings for configured Pascal language provider, 
'        if one exists. 
'   <exe_name> Extension .vb 
'      - Displays settings for the compiler associated with the .vb 
'        file extension. 

Imports System
Imports System.IO
Imports System.Globalization
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp
Imports Microsoft.VisualBasic
Imports System.ComponentModel

Namespace CodeDomCompilerInfoSample

   Class CompilerInfoSample

      <STAThread()>  _
      Public Shared Sub Main(ByVal args() As String)

        Dim queryCommand As String = "" 
        Dim queryArg As String = "" 
        Dim iNumArguments As Integer = args.Length

        ' Get input command-line arguments. 
        If iNumArguments > 0 Then
            queryCommand = args(0).ToUpper(CultureInfo.InvariantCulture)

            If iNumArguments > 1 Then
                queryArg = args(1)
            End If 
        End If 

        ' Determine which method to call.
        Console.WriteLine()
        Select Case queryCommand
            Case "LANGUAGE" 
                ' Display compiler information for input language.
                DisplayCompilerInfoForLanguage(queryArg)
            Case "EXTENSION" 
                ' Display compiler information for input file extension.
                DisplayCompilerInfoUsingExtension(queryArg)
            Case "CONFIG" 
                ' Display settings for the configured language provider.
                DisplayCompilerInfoForConfigLanguage(queryArg)
            Case "ALL" 
                ' Display compiler information for all configured  
                ' language providers.
                DisplayAllCompilerInfo()
            Case Else 
                ' There was no command-line argument, or the  
                ' command-line argument was not recognized. 
                ' Display the C#, Visual Basic and JScript  
                ' compiler information.
                DisplayCSharpCompilerInfo()
                DisplayVBCompilerInfo()
                DisplayJScriptCompilerInfo()
        End Select 

      End Sub 'Main


      Shared Sub DisplayCSharpCompilerInfo()

         ' Get the provider for Microsoft.CSharp 
            Dim provider = CodeDomProvider.CreateProvider("CSharp")

         ' Display the C# language provider information.
         Console.WriteLine("CSharp provider is {0}", _
            provider.ToString())
         Console.WriteLine("  Provider hash code:     {0}", _
            provider.GetHashCode().ToString())
         Console.WriteLine("  Default file extension: {0}", _
            provider.FileExtension)

         Console.WriteLine()
      End Sub 'DisplayCSharpCompilerInfo


      Shared Sub DisplayVBCompilerInfo()
         ' Get the provider for Microsoft.VisualBasic 
            Dim provider = CodeDomProvider.CreateProvider("VisualBasic")

         ' Display the Visual Basic language provider information.
         Console.WriteLine("Visual Basic provider is {0}", _
            provider.ToString())
         Console.WriteLine("  Provider hash code:     {0}", _
            provider.GetHashCode().ToString())
         Console.WriteLine("  Default file extension: {0}", _
            provider.FileExtension)

         Console.WriteLine()
      End Sub 'DisplayVBCompilerInfo


      Shared Sub DisplayJScriptCompilerInfo()
         ' Get the provider for JScript. 
         Dim provider As CodeDomProvider

         Try
            provider = CodeDomProvider.CreateProvider("js")

            ' Display the JScript language provider information.
            Console.WriteLine("JScript language provider is {0}", _
                provider.ToString())
            Console.WriteLine("  Provider hash code:     {0}", _
                provider.GetHashCode().ToString())
            Console.WriteLine("  Default file extension: {0}", _
                provider.FileExtension)
            Console.WriteLine()
         Catch e As System.Configuration.ConfigurationException
            ' The JScript language provider was not found.
            Console.WriteLine("There is no configured JScript language provider.")
         End Try 

      End Sub 'DisplayJScriptCompilerInfo

      Shared Sub DisplayCompilerInfoUsingExtension(fileExtension As String)
         If Not fileExtension.StartsWith(".") Then
            fileExtension = "." + fileExtension
         End If 

         ' Get the language associated with the file extension. 
         If CodeDomProvider.IsDefinedExtension(fileExtension) Then 
            Dim provider As CodeDomProvider
            Dim language As String = CodeDomProvider.GetLanguageFromExtension(fileExtension)

            Console.WriteLine("The language ""{0}"" is associated with file extension ""{1}""", _
                language, fileExtension)
            Console.WriteLine()

            ' Check for a corresponding language provider. 
            If CodeDomProvider.IsDefinedLanguage(language) Then
               provider = CodeDomProvider.CreateProvider(language)

               ' Display information about this language provider.
               Console.WriteLine("Language provider:  {0}", _
                  provider.ToString())
               Console.WriteLine()

               ' Get the compiler settings for this language. 
               Dim langCompilerInfo As CompilerInfo = CodeDomProvider.GetCompilerInfo(language)
               Dim langCompilerConfig As CompilerParameters = langCompilerInfo.CreateDefaultCompilerParameters()

               Console.WriteLine("  Compiler options:        {0}", _
                   langCompilerConfig.CompilerOptions)
               Console.WriteLine("  Compiler warning level:  {0}", _
                   langCompilerConfig.WarningLevel)
            End If 
         Else 
            ' Tell the user that the language provider was not found.
            Console.WriteLine("There is no language provider associated with input file extension ""{0}"".", fileExtension)
         End If 
      End Sub 'DisplayCompilerInfoUsingExtension


      Shared Sub DisplayCompilerInfoForLanguage(language As String)
         Dim provider As CodeDomProvider

         ' Check for a provider corresponding to the input language.   
         If CodeDomProvider.IsDefinedLanguage(language) Then
            provider = CodeDomProvider.CreateProvider(language)

            ' Display information about this language provider.
            Console.WriteLine("Language provider:  {0}", _
                provider.ToString())
            Console.WriteLine()
            Console.WriteLine("  Default file extension:  {0}", _
                provider.FileExtension)
            Console.WriteLine()

            ' Get the compiler settings for this language. 
            Dim langCompilerInfo As CompilerInfo = CodeDomProvider.GetCompilerInfo(language)
            Dim langCompilerConfig As CompilerParameters = langCompilerInfo.CreateDefaultCompilerParameters()

            Console.WriteLine("  Compiler options:        {0}", _
                langCompilerConfig.CompilerOptions)
            Console.WriteLine("  Compiler warning level:  {0}", _
                langCompilerConfig.WarningLevel)
         Else 
            ' Tell the user that the language provider was not found.
            Console.WriteLine("There is no provider configured for input language ""{0}"".", _
                language)
         End If 

      End Sub 'DisplayCompilerInfoForLanguage

      Shared Sub DisplayCompilerInfoForConfigLanguage(configLanguage As String)
         Dim info As CompilerInfo = CodeDomProvider.GetCompilerInfo(configLanguage)

         ' Check whether there is a provider configured for this language. 
         If info.IsCodeDomProviderTypeValid Then 
            ' Get a provider instance using the configured type information. 
            Dim provider As CodeDomProvider
            provider = CType(Activator.CreateInstance(info.CodeDomProviderType), CodeDomProvider)

            ' Display information about this language provider.
            Console.WriteLine("Language provider:  {0}", _
                provider.ToString())
            Console.WriteLine()
            Console.WriteLine("  Default file extension:  {0}", _
                provider.FileExtension)
            Console.WriteLine()

            ' Get the compiler settings for this language. 
            Dim langCompilerConfig As CompilerParameters = info.CreateDefaultCompilerParameters()

            Console.WriteLine("  Compiler options:        {0}", _
                langCompilerConfig.CompilerOptions)
            Console.WriteLine("  Compiler warning level:  {0}", _
                langCompilerConfig.WarningLevel)
         Else 
            ' Tell the user that the language provider was not found.
            Console.WriteLine("There is no provider configured for input language ""{0}"".", configLanguage)
         End If 
      End Sub 'DisplayCompilerInfoForConfigLanguage


      Shared Sub DisplayAllCompilerInfo()
         Dim allCompilerInfo As CompilerInfo() = CodeDomProvider.GetAllCompilerInfo()
         Dim info As CompilerInfo
         For Each info In  allCompilerInfo

            Dim defaultLanguage As String 
            Dim defaultExtension As String 

            Dim provider As CodeDomProvider = info.CreateProvider()

            ' Display information about this configured provider.
            Console.WriteLine("Language provider:  {0}", _
                provider.ToString())
            Console.WriteLine()

            Console.WriteLine("  Supported file extension(s):")
            Dim extension As String 
            For Each extension In info.GetExtensions()
               Console.WriteLine("    {0}", extension)
            Next extension

            defaultExtension = provider.FileExtension
            If Not defaultExtension.StartsWith(".") Then
               defaultExtension = "." + defaultExtension
            End If

            Console.WriteLine("  Default file extension:  {0}", _
              defaultExtension)
            Console.WriteLine()

            Console.WriteLine("  Supported language(s):")
            Dim language As String 
            For Each language In  info.GetLanguages()
               Console.WriteLine("    {0}", language)
            Next language
            defaultLanguage = CodeDomProvider.GetLanguageFromExtension(defaultExtension)
            Console.WriteLine("  Default language:        {0}", _
               defaultLanguage)
            Console.WriteLine()

            ' Get the compiler settings for this provider. 
            Dim langCompilerConfig As CompilerParameters = info.CreateDefaultCompilerParameters()

            Console.WriteLine("  Compiler options:        {0}", _
                langCompilerConfig.CompilerOptions)
            Console.WriteLine("  Compiler warning level:  {0}", _
                langCompilerConfig.WarningLevel)
            Console.WriteLine()
         Next info
      End Sub 'DisplayAllCompilerInfo 

   End Class 'CompilerInfoSample
End Namespace 'CodeDomCompilerInfoSample

System.Object
  System.CodeDom.Compiler.CompilerInfo

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: