CompilerType Clase

Definición

Representa la configuración del compilador utilizada en del entorno de generación ASP.NET para generar y compilar código fuente desde una ruta de acceso virtual. Esta clase no puede heredarse.

public ref class CompilerType sealed
public sealed class CompilerType
type CompilerType = class
Public NotInheritable Class CompilerType
Herencia
CompilerType

Ejemplos

En el ejemplo de código siguiente se muestra una implementación sencilla del proveedor de compilación, que hereda de la clase base abstracta BuildProvider . El proveedor de compilación invalida los CodeCompilerTypemiembros , GetGeneratedTypey GenerateCode de la clase base.

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);
    }
}
Imports System.Collections
Imports System.IO
Imports System.Text
Imports System.Web
Imports System.Web.Compilation
Imports System.CodeDom.Compiler
Imports System.CodeDom
Imports System.Security
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Unrestricted := true)> _
Public Class SampleBuildProvider
    Inherits BuildProvider

    Protected _compilerType As CompilerType = Nothing

    Public Sub New()
        _compilerType = GetDefaultCompilerType()
    End Sub

    ' Return the internal CompilerType member 
    ' defined in this implementation.
    Public Overrides ReadOnly Property CodeCompilerType() As CompilerType
        Get
            CodeCompilerType = _compilerType
        End Get
    End Property

    ' Define a method that returns details for the 
    ' code compiler for this build provider.
    Public Function GetCompilerTypeDetails() As String
        Dim details As StringBuilder = New StringBuilder("")

        If Not _compilerType Is Nothing Then
            ' Format a string that contains the code compiler
            ' implementation, and various compiler details.

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

            If Not _compilerType.CompilerParameters.CompilerOptions Is Nothing Then
                details.AppendFormat("Compiler options: {0}; ", _
                    _compilerType.CompilerParameters.CompilerOptions.ToString())
            End If
        End If
        Return details.ToString()
    End Function

    ' Define the build provider implementation of the GenerateCode method.
    Public Overrides Sub GenerateCode(ByVal assemBuilder As AssemblyBuilder)
        ' Generate a code compile unit, and add it to
        ' the assembly builder.

        Dim tw As TextWriter = assemBuilder.CreateCodeFile(Me)
        If Not tw Is Nothing Then
            Try
                ' Generate the code compile unit from the virtual path.
                Dim compileUnit As CodeCompileUnit = _
                        SampleClassGenerator.BuildCompileUnitFromPath(VirtualPath)

                ' Generate the source for the code compile unit, 
                ' and write it to a file specified by the assembly builder.
                Dim provider As CodeDomProvider = assemBuilder.CodeDomProvider
                provider.CreateGenerator().GenerateCodeFromCompileUnit(compileUnit, tw, Nothing)
            Finally
                tw.Close()
            End Try

        End If
    End Sub

    Public Overrides Function GetGeneratedType(ByVal results As CompilerResults) As System.Type
        Dim typeName As String = SampleClassGenerator.TypeName

        Return results.CompiledAssembly.GetType(typeName)
    End Function

End Class

Comentarios

Use la CodeCompilerType propiedad de una BuildProvider implementación para examinar la configuración utilizada para generar y compilar código fuente a partir de una ruta de acceso virtual para ese proveedor de compilación.

El entorno de compilación de ASP.NET usa BuildProvider objetos para generar código fuente para tipos de archivo personalizados dentro del proyecto. Las clases derivadas de proporcionan detalles de BuildProvider compilación para archivos de origen, páginas web, recursos y otros elementos personalizados. Cuando el proveedor de compilación requiere un lenguaje de programación específico, invalida la CodeCompilerType propiedad y llama al GetDefaultCompilerType método para devolver un CompilerType objeto para el lenguaje de programación admitido.

Para establecer el CompilerType objeto dentro de una implementación del proveedor de compilación, use el GetDefaultCompilerType método o el GetDefaultCompilerTypeForLanguage método .

La CodeDomProviderType propiedad especifica la CodeDomProvider implementación utilizada para generar y compilar código fuente para un proveedor de compilación. La CompilerParameters propiedad define la configuración utilizada para compilar el código fuente en un ensamblado.

Propiedades

CodeDomProviderType

Obtiene un objeto Type para la implementación de CodeDomProvider configurada.

CompilerParameters

Obtiene la configuración y las opciones utilizadas para compilar código fuente en un ensamblado.

Métodos

Equals(Object)

Determina si el objeto especificado representa el mismo proveedor de código y la misma configuración del compilador que la instancia actual de CompilerType.

GetHashCode()

Devuelve el código hash de esta instancia.

GetType()

Obtiene el Type de la instancia actual.

(Heredado de Object)
MemberwiseClone()

Crea una copia superficial del Object actual.

(Heredado de Object)
ToString()

Devuelve una cadena que representa el objeto actual.

(Heredado de Object)

Se aplica a

Consulte también