Επεξεργασία

Κοινή χρήση μέσω


CSharpCodeProvider.GenerateCodeFromMember Method

Definition

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

public:
 override void GenerateCodeFromMember(System::CodeDom::CodeTypeMember ^ member, System::IO::TextWriter ^ writer, System::CodeDom::Compiler::CodeGeneratorOptions ^ options);
public override void GenerateCodeFromMember (System.CodeDom.CodeTypeMember member, System.IO.TextWriter writer, System.CodeDom.Compiler.CodeGeneratorOptions options);
override this.GenerateCodeFromMember : System.CodeDom.CodeTypeMember * System.IO.TextWriter * System.CodeDom.Compiler.CodeGeneratorOptions -> unit
Public Overrides Sub GenerateCodeFromMember (member As CodeTypeMember, writer As TextWriter, options As CodeGeneratorOptions)

Parameters

member
CodeTypeMember

A CodeTypeMember to generate code for.

writer
TextWriter

The TextWriter to write to.

options
CodeGeneratorOptions

The CodeGeneratorOptions to use when generating the code.

Examples

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());
}
    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
End Class

Remarks

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.

Applies to

See also