Szerkesztés

Megosztás a következőn keresztül:


BlobEncoder.MethodSignature Method

Definition

Encodes method signature blob.

public System.Reflection.Metadata.Ecma335.MethodSignatureEncoder MethodSignature (System.Reflection.Metadata.SignatureCallingConvention convention = System.Reflection.Metadata.SignatureCallingConvention.Default, int genericParameterCount = 0, bool isInstanceMethod = false);
member this.MethodSignature : System.Reflection.Metadata.SignatureCallingConvention * int * bool -> System.Reflection.Metadata.Ecma335.MethodSignatureEncoder
Public Function MethodSignature (Optional convention As SignatureCallingConvention = System.Reflection.Metadata.SignatureCallingConvention.Default, Optional genericParameterCount As Integer = 0, Optional isInstanceMethod As Boolean = false) As MethodSignatureEncoder

Parameters

convention
SignatureCallingConvention

Calling convention.

genericParameterCount
Int32

Number of generic parameters.

isInstanceMethod
Boolean

true to encode an instance method signature, false to encode a static method signature.

Returns

An encoder of the rest of the signature including return value and parameters.

Exceptions

genericParameterCount is not in range [0, 0xffff].

Examples

This example shows how to encode different method signatures:

static BlobBuilder EncodeMethodSignatureParameterless()
{
    // void Method()
    var methodSignature = new BlobBuilder();

    new BlobEncoder(methodSignature).
        MethodSignature().
        Parameters(0, returnType => returnType.Void(), parameters => { });
    
    return methodSignature;
}

static BlobBuilder EncodeMethodSignaturePrimitiveTypes()
{
    // double Method(double x, double y)
    var methodSignature = new BlobBuilder();

    new BlobEncoder(methodSignature).
        MethodSignature().
        Parameters(2, returnType => returnType.Type().Double(),
        parameters => {
            parameters.AddParameter().Type().Double();
            parameters.AddParameter().Type().Double();
        });

    return methodSignature;
}

static BlobBuilder EncodeMethodSignatureClassType(MetadataBuilder metadataBuilder)
{
    // void Method(System.Threading.Thread x)
    var methodSignature = new BlobBuilder();
    
    AssemblyReferenceHandle mscorlibAssemblyRef = metadataBuilder.AddAssemblyReference(
        name: metadataBuilder.GetOrAddString("System.Threading.Thread"),
        version: new Version(4, 0, 0, 0),
        culture: default(StringHandle),
        publicKeyOrToken: default(BlobHandle),
        flags: default(AssemblyFlags),
        hashValue: default(BlobHandle));

    TypeReferenceHandle typeRef = metadataBuilder.AddTypeReference(
        mscorlibAssemblyRef,
        metadataBuilder.GetOrAddString("System.Threading"),
        metadataBuilder.GetOrAddString("Thread"));

    new BlobEncoder(methodSignature).
        MethodSignature().
        Parameters(1, returnType => returnType.Void(),
        parameters => {
            parameters.AddParameter().Type().Type(typeRef, false);
        });

    return methodSignature;
}

static BlobBuilder EncodeMethodSignatureModifiedTypes()
{
    // void Method(ref int x, int[] y)
    var methodSignature = new BlobBuilder();

    new BlobEncoder(methodSignature).
        MethodSignature().
        Parameters(2, returnType => returnType.Void(),
        parameters => {
            parameters.AddParameter().Type(isByRef: true).Int32();
            parameters.AddParameter().Type().SZArray().Int32();
        });

    return methodSignature;
}

public static BlobBuilder EncodeMethodSignatureGeneric()
{
    // void Method<T>(T x)
    var methodSignature = new BlobBuilder();

    new BlobEncoder(methodSignature).
        MethodSignature(genericParameterCount: 1).
        Parameters(1, returnType => returnType.Void(),
        parameters => {
            parameters.AddParameter().Type().GenericMethodTypeParameter(0);
        });

    return methodSignature;
}

Remarks

Method signature blobs are used when defining or referencing methods in .NET metadata. For a complete example of emitting a method definition, see the MetadataBuilder class remarks.

Applies to