List languages that you can compile at runtime in C#

[compile at runtime]

The example Run user-entered code in C# shows how to let the user enter C# code, compile at runtime, and execute the result, 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 at runtime 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 =
    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 extensions.
            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 =
            txt += "  Extensions: " + extensions + "\r\n";

            // List supported languages.
            string languages = "";
            string default_language =
            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 =
            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. It then loops through the returned information. For each CompilerInfo object, the program checks the object’s IsCodeDomProviderValid property to make sure the compiler is actually valid on the computer. If the compiler is valid, the code displays information about that compiler.

To display information about a compiler, the code first creates a provider for that 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 at the picture above, you can see that my system supports C#, Visual Basic, JavaScript, and Visual C++. That means that the previous example can compile at runtime code that is written in those languages.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in algorithms, miscellany, programs, reflection and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to List languages that you can compile at runtime in C#

  1. KebTen says:

    Thanks for taking the time to get to the bottom of identifying the CodeDomProvider languages in C# 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.