Skip to content

Commit 60c0bab

Browse files
committed
add example for Smdn.Reflection.ReverseGenerating
1 parent 61db20c commit 60c0bab

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Smdn.Reflection.ReverseGenerating" Version="*" />
11+
<!--
12+
<ProjectReference Include="..\..\..\src\Smdn.Reflection.ReverseGenerating\Smdn.Reflection.ReverseGenerating.csproj" />
13+
-->
14+
</ItemGroup>
15+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Reflection;
2+
using Smdn.Reflection.ReverseGenerating;
3+
4+
var options = new GeneratorOptions();
5+
6+
// Generates the type declaration of int(Int32)
7+
Console.WriteLine(Generator.GenerateTypeDeclaration(typeof(int), null, options));
8+
Console.WriteLine();
9+
10+
// Generates the type declaration of Func<T1, T2, T3, TResult>
11+
Console.WriteLine(Generator.GenerateTypeDeclaration(typeof(Func<,,,>), null, options));
12+
Console.WriteLine();
13+
14+
// Generates the type declaration of Dictionary<TKey, TValue>
15+
Console.WriteLine(Generator.GenerateTypeDeclaration(typeof(Dictionary<,>), null, options));
16+
Console.WriteLine();
17+
18+
// Generates the base interfaces of Dictionary<TKey, TValue>
19+
options.TypeDeclaration.WithNamespace = true;
20+
21+
foreach (var decl in Generator.GenerateExplicitBaseTypeAndInterfaces(typeof(Dictionary<,>), null, options)) {
22+
Console.WriteLine(decl);
23+
}
24+
Console.WriteLine();
25+
26+
// Generates the type declaration of Dictionary<,> with base type, interfaces and generic parameter constraints
27+
foreach (var decl in Generator.GenerateTypeDeclarationWithExplicitBaseTypeAndInterfaces(typeof(Stream), null, options)) {
28+
Console.WriteLine(decl);
29+
}
30+
Console.WriteLine();
31+
32+
// Generates the member declaration of int.TryParse
33+
var intTryParse = typeof(int).GetMethods().First(m => m.Name == "TryParse");
34+
35+
Console.WriteLine(Generator.GenerateMemberDeclaration(intTryParse, null, options));
36+
37+
options.MemberDeclaration.MethodBody = MethodBodyOption.ThrowNull;
38+
options.MemberDeclaration.WithDeclaringTypeName = true;
39+
options.AttributeDeclaration.TypeFilter = (Type attributeType, ICustomAttributeProvider attributeProvider) => false;
40+
41+
Console.WriteLine(Generator.GenerateMemberDeclaration(intTryParse, null, options));
42+
43+
options.MemberDeclaration.MethodBody = MethodBodyOption.None;
44+
options.MemberDeclaration.NullabilityInfoContext = null;
45+
46+
Console.WriteLine(Generator.GenerateMemberDeclaration(intTryParse, null, options));
47+
Console.WriteLine();
48+
49+
// Generates the attribute declarations of List<>
50+
options.AttributeDeclaration.TypeFilter = (Type attributeType, ICustomAttributeProvider attributeProvider) => true;
51+
52+
Console.WriteLine(string.Join(", ", Generator.GenerateAttributeList(typeof(List<>), null, options)));
53+
54+
options.AttributeDeclaration.WithNamespace = true;
55+
options.AttributeDeclaration.OmitAttributeSuffix = false;
56+
57+
Console.WriteLine(string.Join(", ", Generator.GenerateAttributeList(typeof(List<>), null, options)));
58+
59+
Console.WriteLine();

0 commit comments

Comments
 (0)