Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

CompilerInfo Class

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

System.Object
  System.CodeDom.Compiler.CompilerInfo

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

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

The CompilerInfo type exposes the following members.

  NameDescription
Public propertyCodeDomProviderTypeGets the type of the configured CodeDomProvider implementation.
Public propertyIsCodeDomProviderTypeValidReturns a value indicating whether the language provider implementation is configured on the computer.
Top

  NameDescription
Public methodCreateDefaultCompilerParametersGets the configured compiler settings for the language provider implementation.
Public methodCreateProviderReturns a CodeDomProvider instance for the current language provider settings.
Public methodCreateProvider(IDictionary(Of String, String))Returns a CodeDomProvider instance for the current language provider settings and specified options.
Public methodEqualsDetermines whether the specified object represents the same language provider and compiler settings as the current CompilerInfo. (Overrides Object.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetExtensionsReturns the file name extensions supported by the language provider.
Public methodGetHashCodeReturns the hash code for the current instance. (Overrides Object.GetHashCode.)
Public methodGetLanguagesGets the language names supported by the language provider.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

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

Community Additions

Show:
© 2017 Microsoft