Skip to content

Commit 9d9af3d

Browse files
authored
Provide System.Reflection.Context package readme (#106398)
1 parent 96bcf71 commit 9d9af3d

File tree

2 files changed

+134
-6
lines changed

2 files changed

+134
-6
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
## About
2+
3+
Provides the `CustomReflectionContext` class to enable adding or removing custom attributes from reflection objects, or adding dummy properties to those objects, without re-implementing the complete reflection model.
4+
5+
## Key Features
6+
7+
<!-- The key features of this package -->
8+
9+
* Create a custom reflection context to control how types and members are presented during reflection.
10+
* Easily extend or customize the reflection model to fit specialized application needs.
11+
12+
## How to Use
13+
14+
<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package -->
15+
16+
Defining a custom `Attribute` and implement a `CustomReflectionContext` to add the attribute to specic methods.
17+
18+
```csharp
19+
using System.Reflection;
20+
using System.Reflection.Context;
21+
22+
[AttributeUsage(AttributeTargets.Method)]
23+
class CustomAttribute : Attribute
24+
{
25+
}
26+
27+
class CustomContext : CustomReflectionContext
28+
{
29+
// Called whenever the reflection context checks for custom attributes.
30+
protected override IEnumerable<object> GetCustomAttributes(MemberInfo member, IEnumerable<object> declaredAttributes)
31+
{
32+
// Add custom attribute to "ToString" members
33+
if (member.Name == "ToString")
34+
{
35+
yield return new CustomAttribute();
36+
}
37+
38+
// Keep existing attributes as well
39+
foreach (var attr in declaredAttributes)
40+
{
41+
yield return attr;
42+
}
43+
}
44+
}
45+
```
46+
47+
Inspecting the `string` type with both the default and custom reflection context.
48+
49+
```csharp
50+
Type type = typeof(string);
51+
52+
// Representation of the type in the default reflection context
53+
TypeInfo typeInfo = type.GetTypeInfo();
54+
55+
Console.WriteLine("\"To*\" members and their attributes in the default reflection context");
56+
foreach (MemberInfo memberInfo in typeInfo.DeclaredMembers)
57+
{
58+
if (memberInfo.Name.StartsWith("To"))
59+
{
60+
Console.WriteLine(memberInfo.Name);
61+
foreach (Attribute attribute in memberInfo.GetCustomAttributes())
62+
{
63+
Console.WriteLine($" - {attribute.GetType()}");
64+
}
65+
}
66+
}
67+
Console.WriteLine();
68+
69+
// Output:
70+
// "To*" members and their attributes in the default reflection context
71+
// ToCharArray
72+
// ToCharArray
73+
// ToString
74+
// ToString
75+
// ToLower
76+
// ToLower
77+
// ToLowerInvariant
78+
// ToUpper
79+
// ToUpper
80+
// ToUpperInvariant
81+
82+
// Representation of the type in the customized reflection context
83+
CustomContext customContext = new();
84+
TypeInfo customTypeInfo = customContext.MapType(typeInfo);
85+
86+
Console.WriteLine("\"To*\" members and their attributes in the customized reflection context");
87+
foreach (MemberInfo memberInfo in customTypeInfo.DeclaredMembers)
88+
{
89+
if (memberInfo.Name.StartsWith("To"))
90+
{
91+
Console.WriteLine(memberInfo.Name);
92+
foreach (Attribute attribute in memberInfo.GetCustomAttributes())
93+
{
94+
Console.WriteLine($" - {attribute.GetType()}");
95+
}
96+
}
97+
}
98+
99+
// Output:
100+
// "To*" members and their attributes in the customized reflection context
101+
// ToCharArray
102+
// ToCharArray
103+
// ToString
104+
// - CustomAttribute
105+
// ToString
106+
// - CustomAttribute
107+
// ToLower
108+
// ToLower
109+
// ToLowerInvariant
110+
// ToUpper
111+
// ToUpper
112+
// ToUpperInvariant
113+
```
114+
## Main Types
115+
116+
<!-- The main types provided in this library -->
117+
118+
The main type provided by this library is:
119+
120+
* `System.Reflection.Context.CustomReflectionContext`
121+
122+
## Additional Documentation
123+
124+
<!-- Links to further documentation. Remove conceptual documentation if not available for the library. -->
125+
126+
* [API documentation](https://learn.microsoft.com/dotnet/api/system.reflection.context)
127+
128+
## Feedback & Contributing
129+
130+
<!-- How to provide feedback on this package and contribute to it -->
131+
132+
System.Reflection.Context is released as open source under the [MIT license](https://licenses.nuget.org/MIT).
133+
Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/runtime).

src/libraries/System.Reflection.Context/src/System.Reflection.Context.csproj

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
<IsPackable>true</IsPackable>
88
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>
99
<AddNETFrameworkAssemblyReferenceToPackage>true</AddNETFrameworkAssemblyReferenceToPackage>
10-
<PackageDescription>Provides classes that enable customized reflection contexts.
11-
12-
Commonly Used Types:
13-
System.Reflection.Context.CustomReflectionContext</PackageDescription>
14-
<!-- TODO: Add package README file: https://github.com/dotnet/runtime/issues/99358 -->
15-
<EnableDefaultPackageReadmeFile>false</EnableDefaultPackageReadmeFile>
10+
<PackageDescription>Provides CustomReflectionContext to enable customized reflection contexts.</PackageDescription>
1611
</PropertyGroup>
1712

1813
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->

0 commit comments

Comments
 (0)