CodeDomProvider.GetLanguageFromExtension(String) 方法

定义

返回与指定文件扩展名关联的语言名称,如 CodeDomProvider 编译器配置节中所配置的那样。Returns a language name associated with the specified file name extension, as configured in the CodeDomProvider compiler configuration section.

public:
 static System::String ^ GetLanguageFromExtension(System::String ^ extension);
public static string GetLanguageFromExtension (string extension);
[System.Runtime.InteropServices.ComVisible(false)]
public static string GetLanguageFromExtension (string extension);
static member GetLanguageFromExtension : string -> string
[<System.Runtime.InteropServices.ComVisible(false)>]
static member GetLanguageFromExtension : string -> string
Public Shared Function GetLanguageFromExtension (extension As String) As String

参数

extension
String

文件扩展名。A file name extension.

返回

String

与文件扩展名关联的语言名称,如 CodeDomProvider 编译器配置设置中所配置的那样。A language name associated with the file name extension, as configured in the CodeDomProvider compiler configuration settings.

属性

例外

extension 在此计算机上没有已配置的语言提供程序。The extension does not have a configured language provider on this computer.

extensionnullThe extension is null.

调用方没有所要求的权限。The caller does not have the required permission.

示例

下面的代码示例确定 CodeDomProvider 输入文件扩展名的实现并显示语言提供程序的配置设置。The following code example determines the CodeDomProvider implementation for an input file name extension and displays the configured settings for the language provider. 此代码示例是为类提供的更大示例的一部分 CompilerInfoThis code example is part of a larger example provided for the CompilerInfo class.

if (  !fileExtension->StartsWith(  "." ) )
   fileExtension = String::Concat( ".", fileExtension );

// Get the language associated with the file extension.
CodeDomProvider^ provider = nullptr;
if ( CodeDomProvider::IsDefinedExtension( fileExtension ) )
{
   String^ language = CodeDomProvider::GetLanguageFromExtension( fileExtension );
   if ( language )
      Console::WriteLine( "The language \"{0}\" is associated with file extension \"{1}\"\n",
                           language, fileExtension );

   // Check for a corresponding language provider.
   if ( language && CodeDomProvider::IsDefinedLanguage( language ) )
   {
      provider = CodeDomProvider::CreateProvider( language );
      if ( provider )
      {
         // Display information about this language provider.
         Console::WriteLine( "Language provider:  {0}\n", provider->ToString() );
         
         // Get the compiler settings for this language.
         CompilerInfo^ langCompilerInfo = CodeDomProvider::GetCompilerInfo( language );
         if ( langCompilerInfo )
         {
            CompilerParameters^ langCompilerConfig = langCompilerInfo->CreateDefaultCompilerParameters();
            if ( langCompilerConfig )
            {
               Console::WriteLine( "  Compiler options:        {0}", langCompilerConfig->CompilerOptions );
               Console::WriteLine( "  Compiler warning level:  {0}", langCompilerConfig->WarningLevel.ToString() );
            }
         }
      }
   }
}

if ( provider == nullptr )  // Tell the user that the language provider was not found.
   Console::WriteLine( "There is no language provider associated with input file extension \"{0}\".", fileExtension );

if (fileExtension[0] != '.')
{
    fileExtension = "." + fileExtension;
}

// Get the language associated with the file extension.
if (CodeDomProvider.IsDefinedExtension(fileExtension))
{
    CodeDomProvider provider;
    String language = CodeDomProvider.GetLanguageFromExtension(fileExtension);

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

    // Next, check for a corresponding language provider.

    if (CodeDomProvider.IsDefinedLanguage(language))
    {
        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.

        CompilerInfo langCompilerInfo = CodeDomProvider.GetCompilerInfo(language);
        CompilerParameters langCompilerConfig = 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 language provider associated with input file extension \"{0}\".",
        fileExtension);
}
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

注解

计算机配置文件中的 < system.object > 元素 (Machine.config) 包含计算机上每个实现的语言提供程序和编译器配置设置 CodeDomProviderThe <system.codedom> Element in the machine configuration file (Machine.config) contains the language provider and compiler configuration settings for each CodeDomProvider implementation on the computer. GetLanguageFromExtension方法在每个提供程序配置元素中搜索指定的文件扩展名。The GetLanguageFromExtension method searches each provider configuration element for the specified file name extension.

IsDefinedExtension方法检查是否至少有一个提供程序实现支持特定的文件扩展名。The IsDefinedExtension method checks whether at least one provider implementation supports a specific file name extension. 在将文件扩展名传递到之前,可以使用对 IsDefinedExtension 其进行验证 GetLanguageFromExtensionYou can validate a file name extension using IsDefinedExtension before passing it to GetLanguageFromExtension. 这会阻止 GetLanguageFromExtension System.Configuration.ConfigurationException 为不受支持的文件扩展名引发。This prevents GetLanguageFromExtension from throwing a System.Configuration.ConfigurationException for an unsupported file name extension.

如果提供程序实现支持输入文件扩展名,并且为该提供程序配置了多个受支持的语言,则将 GetLanguageFromExtension 返回该提供程序的第一个语言名称。If a provider implementation supports the input file name extension, and there are multiple supported languages configured for that provider, then GetLanguageFromExtension returns the first language name for that provider. 如果为输入文件扩展名配置了多个提供程序实现,则将 GetLanguageFromExtension 返回最后一个匹配提供程序配置元素中的语言名称。If more than one provider implementation is configured for the input file name extension, GetLanguageFromExtension returns the language name from the last matching provider configuration element.

语言名称和文件扩展名不区分大小写。Language names and file name extensions are case-insensitive.

适用于

另请参阅