Export (0) Print
Expand All

CompilerType Class

Represents the compiler settings used within the ASP.NET build environment to generate and compile source code from a virtual path. This class cannot be inherited.

System.Object
  System.Web.Compilation.CompilerType

Namespace:  System.Web.Compilation
Assembly:  System.Web (in System.Web.dll)

public sealed class CompilerType

The CompilerType type exposes the following members.

  NameDescription
Public propertyCodeDomProviderTypeGets a Type for the configured CodeDomProvider implementation.
Public propertyCompilerParametersGets the settings and options used to compile source code into an assembly.
Top

  NameDescription
Public methodEqualsDetermines whether the specified object represents the same code provider and compiler settings as the current instance of CompilerType. (Overrides Object.Equals(Object).)
Public methodGetHashCodeReturns the hash code for this instance. (Overrides Object.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Use the CodeCompilerType property of a BuildProvider implementation to examine the settings used to generate and compile source code from a virtual path for that build provider.

The ASP.NET build environment uses BuildProvider objects to generate source code for custom file types within the project. Classes derived from BuildProvider provide build details for source files, Web pages, resources, and other custom items. When the build provider requires a specific programming language, it overrides the CodeCompilerType property and calls the GetDefaultCompilerType method to return a CompilerType object for the supported programming language.

To set the CompilerType object within a build provider implementation, use the GetDefaultCompilerType method or the GetDefaultCompilerTypeForLanguage method.

The CodeDomProviderType property specifies the CodeDomProvider implementation used to generate and compile source code for a build provider. The CompilerParameters property defines the settings used to compile the source code into an assembly.

The following code example illustrates a simple build provider implementation, inheriting from the abstract BuildProvider base class. The build provider overrides the CodeCompilerType, GetGeneratedType, and GenerateCode members of the base class.

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Compilation;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.Security;
using System.Security.Permissions;

// Define a simple build provider implementation.
[PermissionSet(SecurityAction.Demand, Unrestricted = true)]
public class SampleBuildProvider : BuildProvider
{
    // Define an internal member for the compiler type. 
    protected CompilerType _compilerType = null;

    public SampleBuildProvider()
    {
		// Set the compiler to use Visual Basic.
		_compilerType = GetDefaultCompilerTypeForLanguage("C#");
	}

    // Return the internal CompilerType member  
    // defined in this implementation. 
    public override CompilerType CodeCompilerType
    {
        get { return _compilerType; }
    }

    // Define a method that returns details for the  
    // code compiler for this build provider. 
    public string GetCompilerTypeDetails()
    {
        StringBuilder details = new StringBuilder("");

        if (_compilerType != null)
        {
            // Format a string that contains the code compiler 
            // implementation, and various compiler details.

            details.AppendFormat("CodeDomProvider type: {0}; \n",
                _compilerType.CodeDomProviderType.ToString());
            details.AppendFormat("Compiler debug build = {0}; \n",
                _compilerType.CompilerParameters.IncludeDebugInformation.ToString());
            details.AppendFormat("Compiler warning level = {0}; \n",
                _compilerType.CompilerParameters.WarningLevel.ToString());

            if (_compilerType.CompilerParameters.CompilerOptions != null)
            {
                details.AppendFormat("Compiler options: {0}; \n",
                    _compilerType.CompilerParameters.CompilerOptions.ToString());
            }
        }
        return details.ToString();
    }


    // Define the build provider implementation of the GenerateCode method. 
    public override void GenerateCode(AssemblyBuilder assemBuilder)
    {
        // Generate a code compile unit, and add it to 
        // the assembly builder.

        TextWriter tw = assemBuilder.CreateCodeFile(this);
        if (tw != null)
        {
            try
            {
                // Generate the code compile unit from the virtual path.
                CodeCompileUnit compileUnit = SampleClassGenerator.BuildCompileUnitFromPath(VirtualPath);

                // Generate the source for the code compile unit,  
                // and write it to a file specified by the assembly builder.
                CodeDomProvider provider = assemBuilder.CodeDomProvider;
                provider.CreateGenerator().GenerateCodeFromCompileUnit(compileUnit, tw, null);
            }
            finally
            {
                tw.Close();
            }
        }
    }

    public override System.Type GetGeneratedType(CompilerResults results)
    {
        string typeName = SampleClassGenerator.TypeName;

        return results.CompiledAssembly.GetType(typeName);
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft