BLOG.CSHARPHELPER.COM: List the languages that you can compile with code in C#
List the languages that you can compile with code in C#
The example Compile code entered at run time, execute it, and get the return result in C# shows how to compile code at run time, but how do you know what languages you can compile that way? You might guess C# and Visual Basic but you may be able to compile other languages, too.
This example uses the following code to list the languages that you can compile by using the CodeDomProvider class.
// List compiler supported languages.
private void Form1_Load(object sender, EventArgs e)
{
string txt = "";
// Loop through information about all compilers.
CompilerInfo[] compiler_infos = CodeDomProvider.GetAllCompilerInfo();
foreach (CompilerInfo info in compiler_infos)
{
if (info.IsCodeDomProviderTypeValid)
{
// Get information about this compiler.
CodeDomProvider provider = info.CreateProvider();
txt += "Provider: " + provider.ToString() + "\r\n";
// List supported extentions.
string extensions = "";
string default_extension = provider.FileExtension;
if (default_extension[0] != '.')
default_extension = '.' + default_extension;
foreach (string extension in info.GetExtensions())
{
extensions += ", " + extension;
if (extension == default_extension) extensions += " (default)";
}
if (extensions.Length > 0) extensions = extensions.Substring(2);
txt += " Extensions: " + extensions + "\r\n";
// List supported languages.
string languages = "";
string default_language =
CodeDomProvider.GetLanguageFromExtension(default_extension);
foreach (string language in info.GetLanguages())
{
languages += ", " + language;
if (language == default_language) languages += " (default)";
}
if (languages.Length > 0) languages = languages.Substring(2);
txt += " Languages: " + languages + "\r\n";
// Get the compiler settings for this provider.
CompilerParameters parameters = info.CreateDefaultCompilerParameters();
txt += " Options: " + parameters.CompilerOptions + "\r\n";
txt += " Warning Level: " + parameters.WarningLevel + "\r\n";
}
}
// Display the results.
txtInfo.Text = txt;
txtInfo.Select(0, 0);
}
The program uses the CodeDomProvider class's GetAllCompilerInfo method to get information about available compilers and loops thbrough them. For each CompilerInfo object, the program checks the object's IsCodeDomProviderValid property to make sure the compiler is actually valid on your computer. If the compiler is valid, the code displays information about that compiler.
First the code creates a provider for the compiler and displays its name. It then loops through the provider's supported extensions listing them. It adds the text "(default)" after the provider's default extension.
Next the code performs similar steps to list the compiler's supported languages. These give strings such as C# and CSharp that you can use for the language in the previous example.
Finally the code lists the compiler's default parameters and warning level.
If you look closely at the picture of this example, you can see that my system support C#, Visual Basic, JavaScript, and Visual C++.
Comments