This documentation is archived and is not being maintained.

CodeDomProvider.CreateProvider Method

Gets a CodeDomProvider instance for the specified language.

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

[ComVisibleAttribute(false)] 
public static CodeDomProvider CreateProvider (
	string language
)
/** @attribute ComVisibleAttribute(false) */ 
public static CodeDomProvider CreateProvider (
	String language
)
ComVisibleAttribute(false) 
public static function CreateProvider (
	language : String
) : CodeDomProvider
Not applicable.

Parameters

language

The language name.

Return Value

A CodeDomProvider instance that is implemented for the specified language name.

Exception typeCondition

ConfigurationException

The language does not have a configured provider on this computer.

ConfigurationErrorsException

The language is a null reference (Nothing in Visual Basic).

SecurityException

The caller does not have the required permission.

NoteNote:

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 allows you to specify at run time the code provider you wish to instantiate. 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.

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. Use CreateProvider when you want to dynamically find a configured provider implementation for a language name.

If more than one provider implementation is configured for the language name, CreateProvider returns a provider instance for the last matching configuration element.

Use the Activator.CreateInstance method overload when you want a specific language provider implementation. For example, use the CreateProvider method to get a provider instance that supports the language name "CSharp"; use the Activator.CreateInstance method overload to get a provider instance specifically for the Microsoft.CSharp.CSharpCodeProvider implementation. You should use the Activator.CreateInstance method if you have multiple code providers for a language and you desire to instantiate a specific code provider.

The IsDefinedLanguage method checks whether at least one provider implementation supports a specific language. You can validate a language name using IsDefinedLanguage before passing it to CreateProvider. If you pass an unsupported language name to CreateProvider a System.Configuration.ConfigurationException is thrown.

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).

The CreateProvider method returns an instance of a CodeDomProvider implementation for a specific language.

Language names are case-insensitive.

The following code example determines the CodeDomProvider implementation for an input language and displays the configured settings for the language provider.

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

  • SecurityAction.LinkDemand  for full trust for the immediate caller. This member cannot be used by partially trusted code.

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Show: