CodeDomProvider.CreateProvider 方法

定义

获取指定语言的 CodeDomProvider 实例。Gets a CodeDomProvider instance for the specified language.

重载

CreateProvider(String)

获取指定语言的 CodeDomProvider 实例。Gets a CodeDomProvider instance for the specified language.

CreateProvider(String, IDictionary<String,String>)

获取指定的语言和提供程序选项的 CodeDomProvider 实例。Gets a CodeDomProvider instance for the specified language and provider options.

CreateProvider(String)

获取指定语言的 CodeDomProvider 实例。Gets a CodeDomProvider instance for the specified language.

public:
 static System::CodeDom::Compiler::CodeDomProvider ^ CreateProvider(System::String ^ language);
public static System.CodeDom.Compiler.CodeDomProvider CreateProvider (string language);
[System.Runtime.InteropServices.ComVisible(false)]
public static System.CodeDom.Compiler.CodeDomProvider CreateProvider (string language);
static member CreateProvider : string -> System.CodeDom.Compiler.CodeDomProvider
[<System.Runtime.InteropServices.ComVisible(false)>]
static member CreateProvider : string -> System.CodeDom.Compiler.CodeDomProvider
Public Shared Function CreateProvider (language As String) As CodeDomProvider

参数

language
String

语言名称。The language name.

返回

CodeDomProvider

为指定语言名称实现的 CodeDOM 提供程序。A CodeDOM provider that is implemented for the specified language name.

属性

例外

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

languagenullThe language is null.

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

示例

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

CodeDomProvider^ provider = nullptr;

// Check for a provider corresponding to the input language.  
if ( CodeDomProvider::IsDefinedLanguage( language ) )
{
   provider = CodeDomProvider::CreateProvider( language );
   if ( provider )
   {
      // 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.
      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 provider configured for input language \"{0}\".", language );

CodeDomProvider provider;

// Check for a provider corresponding to the input language.
if (CodeDomProvider.IsDefinedLanguage(language))
{
    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.

    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 provider configured for input language \"{0}\".",
        language);
}
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

注解

备注

此方法最常用于在应用程序中创建代码提供程序的实例,该实例可以选择使用多个提供程序之一。This method is most commonly used to create an instance of a code provider in an application that may optionally use one of several providers. CreateProvider 允许你在运行时指定要实例化的代码提供程序。CreateProvider allows you to specify at run time the code provider you wish to instantiate. 如果您在设计时知道要使用哪一个代码提供程序,则应创建该代码提供程序的实例,而不是使用 CreateProvider 方法。If you know at design time which code provider is to be used, you should create an instance of that code provider rather than use the CreateProvider method.

CreateProvider方法返回 CodeDomProvider 特定语言名称的实例; 与 Activator.CreateInstance 使用语言提供程序类型调用方法类似。The CreateProvider method returns a CodeDomProvider instance for a specific language name; it is similar to calling the Activator.CreateInstance method with the language provider type. CreateProvider需要动态查找语言名称的已配置提供程序实现时,请使用。Use CreateProvider when you want to dynamically find a configured provider implementation for a language name.

如果为语言名称配置了多个提供程序实现,则 CreateProvider 返回最后一个匹配配置元素的提供程序实例。If more than one provider implementation is configured for the language name, CreateProvider returns a provider instance for the last matching configuration element.

Activator.CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo)如果需要特定的语言提供程序实现,请使用方法重载。Use the Activator.CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo) method overload when you want a specific language provider implementation. 例如,使用 CreateProvider 方法获取支持语言名称的提供程序实例 "CSharp" ; 使用 Activator.CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo) 方法重载获取专用于实现的提供程序实例 Microsoft.CSharp.CSharpCodeProviderFor example, use the CreateProvider method to get a provider instance that supports the language name "CSharp"; use the Activator.CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo) method overload to get a provider instance specifically for the Microsoft.CSharp.CSharpCodeProvider implementation. Activator.CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo, Object[])如果有多个用于语言的代码提供程序并且需要实例化特定的代码提供程序,请使用方法。Use the Activator.CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo, Object[]) method if you have multiple code providers for a language and you desire to instantiate a specific code provider.

IsDefinedLanguage方法检查是否至少有一个提供程序实现支持特定语言。The IsDefinedLanguage method checks whether at least one provider implementation supports a specific language. 在将语言名称传递给之前,可以使用对 IsDefinedLanguage 其进行验证 CreateProviderYou can validate a language name using IsDefinedLanguage before passing it to CreateProvider. 如果将不支持的语言名称传递 CreateProviderSystem.Configuration.ConfigurationException ,则会引发。If you pass an unsupported language name to CreateProvider a System.Configuration.ConfigurationException is thrown.

GetAllCompilerInfo方法可用于确定计算机上的所有 CodeDomProvider 实现,包括开发人员和编译器供应商提供的、在计算机配置文件 (Machine.config) 中 < > 标识的其他实现。The GetAllCompilerInfo method can be used to determine all CodeDomProvider implementations on a computer, including additional implementations provided by developers and compiler vendors that are identified in the <system.codedom> Element in the machine configuration file (Machine.config).

CreateProvider方法返回 CodeDomProvider 特定语言的实现的实例。The CreateProvider method returns an instance of a CodeDomProvider implementation for a specific language.

语言名称不区分大小写。Language names are case-insensitive.

另请参阅

适用于

CreateProvider(String, IDictionary<String,String>)

获取指定的语言和提供程序选项的 CodeDomProvider 实例。Gets a CodeDomProvider instance for the specified language and provider options.

public:
 static System::CodeDom::Compiler::CodeDomProvider ^ CreateProvider(System::String ^ language, System::Collections::Generic::IDictionary<System::String ^, System::String ^> ^ providerOptions);
public static System.CodeDom.Compiler.CodeDomProvider CreateProvider (string language, System.Collections.Generic.IDictionary<string,string> providerOptions);
[System.Runtime.InteropServices.ComVisible(false)]
public static System.CodeDom.Compiler.CodeDomProvider CreateProvider (string language, System.Collections.Generic.IDictionary<string,string> providerOptions);
static member CreateProvider : string * System.Collections.Generic.IDictionary<string, string> -> System.CodeDom.Compiler.CodeDomProvider
[<System.Runtime.InteropServices.ComVisible(false)>]
static member CreateProvider : string * System.Collections.Generic.IDictionary<string, string> -> System.CodeDom.Compiler.CodeDomProvider
Public Shared Function CreateProvider (language As String, providerOptions As IDictionary(Of String, String)) As CodeDomProvider

参数

language
String

语言名称。The language name.

providerOptions
IDictionary<String,String>

来自配置文件的提供程序选项的集合。A collection of provider options from the configuration file.

返回

CodeDomProvider

为指定语言名称和选项实现的 CodeDOM 提供程序。A CodeDOM provider that is implemented for the specified language name and options.

属性

示例

下面的示例演示如何使用参数创建提供程序的实例 providerOptionsThe following example shows how to create an instance of a provider by using the providerOptions parameter.

using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
using System.Collections.Generic;

namespace ProviderOptions
{
    class Program
    {
        static void Main(string[] args)
        {
            DisplayCSharpCompilerInfo();
            DisplayVBCompilerInfo();
            Console.WriteLine("Press Enter key to exit.");
            Console.ReadLine();
        }
        static void DisplayCSharpCompilerInfo()
        {
            Dictionary<string, string> provOptions =
            new Dictionary<string, string>();

            provOptions.Add("CompilerVersion", "v4");
            // Get the provider for Microsoft.CSharp
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp", provOptions);

            // 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();
        }

        static void DisplayVBCompilerInfo()
        {
            Dictionary<string, string> provOptions =
            new Dictionary<string, string>();

            provOptions.Add("CompilerVersion", "v3.5");
            // Get the provider for Microsoft.VisualBasic
            CodeDomProvider provider = CodeDomProvider.CreateProvider("VisualBasic", provOptions);

            // 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();
        }
    }
}
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp
Imports System.Collections.Generic



Class Program

    Shared Sub Main(ByVal args() As String)
        DisplayCSharpCompilerInfo()
        DisplayVBCompilerInfo()
        Console.WriteLine("Press Enter key to exit.")
        Console.ReadLine()

    End Sub

    Shared Sub DisplayCSharpCompilerInfo()
        Dim provOptions As New Dictionary(Of String, String)
        provOptions.Add("CompilerVersion", "v4")
        ' Get the provider for Microsoft.CSharp
        Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("CSharp", provOptions)

        ' 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


    Shared Sub DisplayVBCompilerInfo()
        Dim provOptions As New Dictionary(Of String, String)
        provOptions.Add("CompilerVersion", "v3.5")
        ' Get the provider for Microsoft.VisualBasic
        Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic", provOptions)

        ' 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
End Class

注解

备注

此方法最常用于在应用程序中创建代码提供程序的实例,该实例可以选择使用多个提供程序之一。This method is most commonly used to create an instance of a code provider in an application that may optionally use one of several providers. CreateProvider(String, IDictionary<String,String>) 使你能够在运行时指定要实例化的代码提供程序的版本。CreateProvider(String, IDictionary<String,String>) enables you to specify at run time the version of the code provider you want to instantiate. 如果在设计时知道要使用哪种代码提供程序,则应创建该代码提供程序的实例,而不是使用 CreateProvider(String, IDictionary<String,String>) 方法。If you know at design time which code provider is to be used, you should create an instance of that code provider instead of using the CreateProvider(String, IDictionary<String,String>) method.

CreateProvider(String, IDictionary<String,String>)当你想要针对特定语言和选项动态查找配置的提供程序实现时,请使用。Use CreateProvider(String, IDictionary<String,String>) when you want to dynamically find a configured provider implementation for a specific language and options. 语言名称不区分大小写。Language names are case-insensitive. 有关支持的提供程序选项的信息,请参阅特定的 CodeDOM 提供程序文档。For information about supported provider options, see the specific CodeDOM provider documentation.

有关为语言名称配置了多个提供程序实现的验证提供程序和调用提供程序的信息,请参见方法的 "备注" 部分 CreateProvider(String)For information about validating a provider and calling a provider if more than one provider implementation is configured for the language name, see the Remarks section of the CreateProvider(String) method.

适用于