CodeDomProvider..::.GenerateCodeFromMember Method
This page is specific to:.NET Framework Version:2.03.03.54.0
.NET Framework Class Library
CodeDomProvider..::.GenerateCodeFromMember Method

Generates code for the specified Code Document Object Model (CodeDOM) member declaration and sends it to the specified text writer, using the specified options.

Namespace:  System.CodeDom.Compiler
Assembly:  System (in System.dll)
Syntax

'Usage

Dim instance As CodeDomProvider
Dim member As CodeTypeMember
Dim writer As TextWriter
Dim options As CodeGeneratorOptions

instance.GenerateCodeFromMember(member, _
    writer, options)

'Declaration

Public Overridable Sub GenerateCodeFromMember ( _
    member As CodeTypeMember, _
    writer As TextWriter, _
    options As CodeGeneratorOptions _
)

Parameters

member
Type: System.CodeDom..::.CodeTypeMember
A CodeTypeMember object that indicates the member for which to generate code.
writer
Type: System.IO..::.TextWriter
The TextWriter to which output code is sent.
options
Type: System.CodeDom.Compiler..::.CodeGeneratorOptions
A CodeGeneratorOptions that indicates the options to use for generating code.
Exceptions

ExceptionCondition
NotImplementedException

This method is not overridden in a derived class.

Remarks

The base class implementation throws a NotImplementedException. See CSharpCodeProvider..::.GenerateCodeFromMember for documentation describing an implementation of this method.

Examples

The following code example shows the use of the GenerateCodeFromMember method as implemented by the CSharpCodeProvider and VBCodeProvider classes.

Imports System
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.IO
Imports System.Text.RegularExpressions



Class Program
    Private Shared providerName As String = "vb"
    Private Shared sourceFileName As String = "test.vb"
    Private Shared snippetMethod As CodeSnippetTypeMember

    Shared Sub Main(ByVal args() As String) 
        Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider(providerName)

        ' Create a code snippet to be used in the graph.
        GenCodeFromMember(provider, New CodeGeneratorOptions())

        LogMessage("Building CodeDOM graph...")

        Dim cu As New CodeCompileUnit()

        cu = BuildClass1()

        Dim sw As New StringWriter()

        LogMessage("Generating code...")
        provider.GenerateCodeFromCompileUnit(cu, sw, Nothing)

        Dim output As String = sw.ToString()

        LogMessage("Dumping source...")
        LogMessage(output)

        LogMessage("Writing source to file...")
        Dim s As Stream = File.Open(sourceFileName, FileMode.Create)
        Dim t As New StreamWriter(s)
        t.Write(output)
        t.Close()
        s.Close()

        Dim opt As New CompilerParameters(New String() {"System.dll"})
        opt.GenerateExecutable = False
        opt.OutputAssembly = "Sample.dll"

        Dim results As CompilerResults

        LogMessage(("Compiling with " + providerName))
        results = provider.CompileAssemblyFromFile(opt, sourceFileName)

        OutputResults(results)
        If results.NativeCompilerReturnValue <> 0 Then
            LogMessage("")
            LogMessage("Compilation failed.")
        Else
            LogMessage("")
            LogMessage("Demo completed successfully.")
        End If
        File.Delete(sourceFileName)

    End Sub 'Main


    ' Build a library program graph using 
    ' System.CodeDom types.
    Public Shared Function BuildClass1() As CodeCompileUnit 
        ' Create a new CodeCompileUnit to contain 
        ' the program graph.
        Dim compileUnit As New CodeCompileUnit()

        ' Declare a new namespace called Samples.
        Dim samples As New CodeNamespace("Samples")
        ' Add the new namespace to the compile unit.
        compileUnit.Namespaces.Add(samples)

        ' Add the new namespace import for the System namespace.
        samples.Imports.Add(New CodeNamespaceImport("System"))

        ' Declare a new type called Class1.
        Dim class1 As New CodeTypeDeclaration("Class1")

        ' Add the new type to the namespace type collection.
        samples.Types.Add(class1)

        class1.Members.Add(snippetMethod)

        Return compileUnit

    End Function 'BuildClass1

    Shared Sub LogMessage(ByVal [text] As String) 
        Console.WriteLine([text])

    End Sub 'LogMessage


    Shared Sub OutputResults(ByVal results As CompilerResults) 
        LogMessage(("NativeCompilerReturnValue=" + results.NativeCompilerReturnValue.ToString()))
        Dim s As String
        For Each s In  results.Output
            LogMessage(s)
        Next s

    End Sub 'OutputResults

    Shared Sub GenCodeFromMember(ByVal provider As CodeDomProvider, ByVal options As CodeGeneratorOptions) 
        options.BracingStyle = "C"
        Dim method1 As New CodeMemberMethod()
        method1.Name = "ReturnString"
        method1.Attributes = MemberAttributes.Public
        method1.ReturnType = New CodeTypeReference("System.String")
        method1.Parameters.Add(New CodeParameterDeclarationExpression("System.String", "text"))
        method1.Statements.Add(New CodeMethodReturnStatement(New CodeArgumentReferenceExpression("text")))
        Dim sw As New StringWriter()
        provider.GenerateCodeFromMember(method1, sw, options)
        snippetMethod = New CodeSnippetTypeMember(sw.ToString())

    End Sub 'GenCodeFromMember
End Class 'Program 


Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View