BuildProvider.GenerateCode(AssemblyBuilder) 메서드

정의

빌드 공급자의 가상 경로에 대해 소스 코드를 생성하고 이 소스 코드를 지정된 어셈블리 빌더에 추가합니다.

public:
 virtual void GenerateCode(System::Web::Compilation::AssemblyBuilder ^ assemblyBuilder);
public virtual void GenerateCode (System.Web.Compilation.AssemblyBuilder assemblyBuilder);
abstract member GenerateCode : System.Web.Compilation.AssemblyBuilder -> unit
override this.GenerateCode : System.Web.Compilation.AssemblyBuilder -> unit
Public Overridable Sub GenerateCode (assemblyBuilder As AssemblyBuilder)

매개 변수

assemblyBuilder
AssemblyBuilder

빌드 공급자에서 생성된 소스 코드를 참조하는 어셈블리 작성기입니다.

예제

다음 코드 예제에서는 추상에서 상속 하는 간단한 빌드 공급자 구현 BuildProvider 기본 클래스입니다. 빌드 공급자를 재정의 합니다 CodeCompilerType, GetGeneratedType, 및 GenerateCode 기본 클래스의 멤버입니다.

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()
    {
        _compilerType = GetDefaultCompilerTypeForLanguage("C#");
    }

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

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

설명

사용자 지정 파일 형식에 대한 소스 코드를 생성하는 빌드 공급자를 구현하려면 클래스에서 클래스를 BuildProvider 파생시키고 메서드를 재정의 GenerateCode 하여 지원되는 파일 형식에 대한 소스 코드를 생성합니다.

일반적으로 빌드 공급자의 GenerateCode 구현은 속성을 읽고 VirtualPath , 내용을 구문 분석하고, 생성된 소스 코드를 지정된 AssemblyBuilder 개체에 추가합니다. 소스 코드 형태로 어셈블리에 기여할 수 있는 TextWriter CodeDOM 그래프 또는 개체입니다. 개체를 통해 TextWriter 어셈블리에 소스 코드를 추가하려면 메서드를 CreateCodeFile 사용합니다. CodeDOM 그래프의 소스 코드를 어셈블리에 추가하려면 이 메서드를 AddCodeCompileUnit 사용합니다.

빌드 공급자가 특정 프로그래밍 언어로 소스 코드를 생성하는 경우 지원되는 프로그래밍 언어에 대한 개체를 반환 CompilerType 하도록 속성을 재정 CodeCompilerType 의해야 합니다.

적용 대상

추가 정보