CSharpCodeProvider.GenerateCodeFromMember Method (CodeTypeMember, TextWriter, CodeGeneratorOptions)

 

Generates code for the specified class member using the specified text writer and code generator options.

Namespace:   Microsoft.CSharp
Assembly:  System (in System.dll)

public override void GenerateCodeFromMember(
	CodeTypeMember member,
	TextWriter writer,
	CodeGeneratorOptions options
)

Parameters

member
Type: System.CodeDom.CodeTypeMember

A CodeTypeMember to generate code for.

writer
Type: System.IO.TextWriter

The TextWriter to write to.

options
Type: System.CodeDom.Compiler.CodeGeneratorOptions

The CodeGeneratorOptions to use when generating the code.

Members are generated differently depending on their context. For example, a method in an interface or a field in an enumeration is generated differently than a method or field on a class. This implementation generates code for a member in the class context.

The following code example shows the use of the GenerateCodeFromMember method to create a code fragment to be used in a CodeDOM graph. This code example is part of a larger example provided for the GenerateCodeFromMember method of the CodeDomProvider class.

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
Available since 2.0
Return to top
Show: