BuildProvider.GenerateCode Method (AssemblyBuilder)
Generates source code for the virtual path of the build provider, and adds the source code to a specified assembly builder.
Assembly: System.Web (in System.Web.dll)
Parameters
- assemblyBuilder
-
Type:
System.Web.Compilation.AssemblyBuilder
The assembly builder that references the source code generated by the build provider.
To implement a build provider that generates source code for a custom file type, derive a class from the BuildProvider class, and override the GenerateCode method to generate source code for the supported file type.
Typically, a build provider's GenerateCode implementation reads the VirtualPath property, parses the contents, and adds the generated source code to the specified AssemblyBuilder object. You can contribute source code to the assembly in the form of a TextWriter object or a CodeDOM graph. To add source code to the assembly through a TextWriter object, use the CreateCodeFile method. To add source code in a CodeDOM graph to the assembly, use the AddCodeCompileUnit method.
If the build provider generates source code in a specific programming language, you must override the CodeCompilerType property to return a CompilerType object for the supported programming language.
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.
Imports Microsoft.VisualBasic Imports System 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
Available since 2.0