BlobEncoder.MethodSignature 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將方法簽章 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 類別備註。