BlobEncoder.MethodSignature 方法

定义

对方法签名 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

参数

convention
SignatureCallingConvention

调用约定。

genericParameterCount
Int32

泛型形参的数量。

isInstanceMethod
Boolean

若要对实例方法签名进行编码,则为 true;若要对静态方法签名进行编码,则为 false

返回

签名的其余部分(包括返回值和参数)的编码器。

例外

genericParameterCount 不在范围 [0, 0xffff] 内。

示例

此示例演示如何对不同的方法签名进行编码:

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;
}

注解

在 .NET 元数据中定义或引用方法时,使用方法签名 Blob。 有关发出方法定义的完整示例,请参阅 MetadataBuilder 类注释。

适用于