This documentation is archived and is not being maintained.

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)

public virtual void GenerateCodeFromMember(
	CodeTypeMember member,
	TextWriter writer,
	CodeGeneratorOptions options


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


This method is not overridden in a derived class.

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

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

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Text.RegularExpressions;

namespace BasicCodeDomApp
    class Program
        static string providerName = "cs";
        static string sourceFileName = "test.cs";
        static CodeSnippetTypeMember snippetMethod;
        static void Main(string[] args)
            CodeDomProvider provider = CodeDomProvider.CreateProvider(providerName);

            // Create a code snippet to be used in the graph.
            GenCodeFromMember(provider, new CodeGeneratorOptions());

            LogMessage("Building CodeDOM graph...");

            CodeCompileUnit cu = new CodeCompileUnit();

            cu = BuildClass1();

            StringWriter sw = new StringWriter();

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

            string output = sw.ToString();

            LogMessage("Dumping source...");

            LogMessage("Writing source to file...");
            Stream s = File.Open(sourceFileName, FileMode.Create);
            StreamWriter t = new StreamWriter(s);

            CompilerParameters opt = new CompilerParameters(new string[]{
                                      "System.dll" });
            opt.GenerateExecutable = false;
            opt.OutputAssembly = "Sample.dll";

            CompilerResults results;

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

            if (results.NativeCompilerReturnValue != 0)
                LogMessage("Compilation failed.");
                LogMessage("Demo completed successfully.");

        // Build a library program graph using 
        // System.CodeDom types.
        public static CodeCompileUnit BuildClass1()
            // Create a new CodeCompileUnit to contain 
            // the program graph.
            CodeCompileUnit compileUnit = new CodeCompileUnit();

            // Declare a new namespace called Samples.
            CodeNamespace samples = new CodeNamespace("Samples");
            // Add the new namespace to the compile unit.

            // Add the new namespace import for the System namespace.
            samples.Imports.Add(new CodeNamespaceImport("System"));

            // Declare a new type called Class1.
            CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");

            // Add the new type to the namespace type collection.


            return compileUnit;
        static void LogMessage(string text)

        static void OutputResults(CompilerResults results)
            LogMessage("NativeCompilerReturnValue=" +
            foreach (string s in results.Output)
        static void GenCodeFromMember(CodeDomProvider provider, CodeGeneratorOptions options)
            options.BracingStyle = "C";
            CodeMemberMethod method1 = 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")));
            StringWriter sw = new StringWriter();
            provider.GenerateCodeFromMember(method1, sw, options);
            snippetMethod = new CodeSnippetTypeMember(sw.ToString());


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.