다음을 통해 공유


AssemblyBuilder.CreateCodeFile(BuildProvider) 메서드

정의

빌드 공급자가 임시 소스 파일을 만들고 어셈블리 컴파일에 원본 파일을 포함할 수 있습니다.

public:
 System::IO::TextWriter ^ CreateCodeFile(System::Web::Compilation::BuildProvider ^ buildProvider);
public System.IO.TextWriter CreateCodeFile(System.Web.Compilation.BuildProvider buildProvider);
member this.CreateCodeFile : System.Web.Compilation.BuildProvider -> System.IO.TextWriter
Public Function CreateCodeFile (buildProvider As BuildProvider) As TextWriter

매개 변수

buildProvider
BuildProvider

코드 소스 파일을 생성하는 빌드 공급자입니다.

반환

임시 파일에 소스 코드를 작성하는 데 사용할 수 있는 열기 TextWriter 입니다.

예제

다음 코드 예제에서는 추상 BuildProvider 기본 클래스에서 상속되는 간단한 빌드 공급자 구현을 보여 줍니다. 빌드 공급자는 기본 클래스의 CodeCompilerTypeGetGeneratedTypeGenerateCode 멤버를 재정의합니다.

GenerateCode 메서드 구현에서 빌드 공급자는 메서드를 사용하여 CreateCodeFile 어셈블리 컴파일에 대해 생성된 코드를 추가합니다. 이 예제에는 클래스의 구현이 SampleClassGenerator 포함되어 있지 않습니다. 자세한 내용은 CodeCompileUnit를 참조하세요.

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

설명

BuildProvider 구현은 가상 경로에 CreateCodeFile 대한 원본 파일을 생성할 때 메서드를 호출합니다. 추가 CreateCodeFile 된 소스 코드는 어셈블리 컴파일에 포함됩니다.

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

호출 CreateCodeFile후 빌드 공급자는 반환 TextWriter 된 개체를 사용하여 원본 파일 콘텐츠를 작성합니다. 원본 파일을 작성한 후 개체는 BuildProvider 메서드를 Close 사용하여 개체를 TextWriter 닫고 연결된 시스템 리소스를 해제해야 합니다.

적용 대상

추가 정보