Skip to content

Commit 0b39a8f

Browse files
Add example for BlobEncoder.MethodSignature (#7678)
* Create MethodSignatureSnippets.cs * Add project * Update BlobEncoder.xml * Update xml/System.Reflection.Metadata.Ecma335/BlobEncoder.xml Co-authored-by: Genevieve Warren <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 2ce621c commit 0b39a8f

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Reflection.Metadata;
3+
using System.Reflection.Metadata.Ecma335;
4+
using System.Reflection;
5+
6+
namespace MethodSignatureSnippets
7+
{
8+
static class MethodSignatureSnippets
9+
{
10+
// <SnippetEncodeSignatures>
11+
static BlobBuilder EncodeMethodSignatureParameterless()
12+
{
13+
// void Method()
14+
var methodSignature = new BlobBuilder();
15+
16+
new BlobEncoder(methodSignature).
17+
MethodSignature().
18+
Parameters(0, returnType => returnType.Void(), parameters => { });
19+
20+
return methodSignature;
21+
}
22+
23+
static BlobBuilder EncodeMethodSignaturePrimitiveTypes()
24+
{
25+
// double Method(double x, double y)
26+
var methodSignature = new BlobBuilder();
27+
28+
new BlobEncoder(methodSignature).
29+
MethodSignature().
30+
Parameters(2, returnType => returnType.Type().Double(),
31+
parameters => {
32+
parameters.AddParameter().Type().Double();
33+
parameters.AddParameter().Type().Double();
34+
});
35+
36+
return methodSignature;
37+
}
38+
39+
static BlobBuilder EncodeMethodSignatureClassType(MetadataBuilder metadataBuilder)
40+
{
41+
// void Method(System.Threading.Thread x)
42+
var methodSignature = new BlobBuilder();
43+
44+
AssemblyReferenceHandle mscorlibAssemblyRef = metadataBuilder.AddAssemblyReference(
45+
name: metadataBuilder.GetOrAddString("System.Threading.Thread"),
46+
version: new Version(4, 0, 0, 0),
47+
culture: default(StringHandle),
48+
publicKeyOrToken: default(BlobHandle),
49+
flags: default(AssemblyFlags),
50+
hashValue: default(BlobHandle));
51+
52+
TypeReferenceHandle typeRef = metadataBuilder.AddTypeReference(
53+
mscorlibAssemblyRef,
54+
metadataBuilder.GetOrAddString("System.Threading"),
55+
metadataBuilder.GetOrAddString("Thread"));
56+
57+
new BlobEncoder(methodSignature).
58+
MethodSignature().
59+
Parameters(1, returnType => returnType.Void(),
60+
parameters => {
61+
parameters.AddParameter().Type().Type(typeRef, false);
62+
});
63+
64+
return methodSignature;
65+
}
66+
67+
static BlobBuilder EncodeMethodSignatureModifiedTypes()
68+
{
69+
// void Method(ref int x, int[] y)
70+
var methodSignature = new BlobBuilder();
71+
72+
new BlobEncoder(methodSignature).
73+
MethodSignature().
74+
Parameters(2, returnType => returnType.Void(),
75+
parameters => {
76+
parameters.AddParameter().Type(isByRef: true).Int32();
77+
parameters.AddParameter().Type().SZArray().Int32();
78+
});
79+
80+
return methodSignature;
81+
}
82+
83+
public static BlobBuilder EncodeMethodSignatureGeneric()
84+
{
85+
// void Method<T>(T x)
86+
var methodSignature = new BlobBuilder();
87+
88+
new BlobEncoder(methodSignature).
89+
MethodSignature(genericParameterCount: 1).
90+
Parameters(1, returnType => returnType.Void(),
91+
parameters => {
92+
parameters.AddParameter().Type().GenericMethodTypeParameter(0);
93+
});
94+
95+
return methodSignature;
96+
}
97+
// </SnippetEncodeSignatures>
98+
99+
public static void Run()
100+
{
101+
var metadataBuilder = new MetadataBuilder();
102+
EncodeMethodSignatureParameterless();
103+
EncodeMethodSignaturePrimitiveTypes();
104+
EncodeMethodSignatureClassType(metadataBuilder);
105+
EncodeMethodSignatureModifiedTypes();
106+
EncodeMethodSignatureGeneric();
107+
}
108+
}
109+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace MethodSignatureSnippets
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
MethodSignatureSnippets.Run();
10+
}
11+
}
12+
}

xml/System.Reflection.Metadata.Ecma335/BlobEncoder.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,20 @@
244244
<see langword="true" /> to encode an instance method signature, <see langword="false" /> to encode a static method signature.</param>
245245
<summary>Encodes method signature blob.</summary>
246246
<returns>An encoder of the rest of the signature including return value and parameters.</returns>
247-
<remarks>To be added.</remarks>
247+
<remarks>
248+
<format type="text/markdown"><![CDATA[
249+
250+
## Remarks
251+
252+
Method signature blobs are used when defining or referencing methods in .NET metadata. For a complete example of emitting a method definition, see the <xref:System.Reflection.Metadata.Ecma335.MetadataBuilder> class remarks.
253+
254+
## Examples
255+
This example shows how to encode different method signatures:
256+
257+
:::code language="csharp" source="~/snippets/csharp/System.Reflection.Metadata.Ecma335/BlobEncoder/MethodSignature/MethodSignatureSnippets.cs" id="SnippetEncodeSignatures":::
258+
259+
]]></format>
260+
</remarks>
248261
<exception cref="T:System.ArgumentOutOfRangeException">
249262
<paramref name="genericParameterCount" /> is not in range [0, 0xffff].</exception>
250263
</Docs>

0 commit comments

Comments
 (0)