Skip to content

Commit 6e6a514

Browse files
Add note on suppressing trimming warnings
Inspired by the suppression justification from dotnet/linker#2891.
1 parent 3c3d231 commit 6e6a514

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

docs/core/deploying/trimming/prepare-libraries-for-trimming.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class TypeCollection
207207
{
208208
Type[] types;
209209

210-
// Ensure that only types with ctors are stored in the array
210+
// Ensure that only types with preserved constructors are stored in the array
211211
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
212212
public Type this[int i]
213213
{
@@ -249,6 +249,27 @@ public Type this[int i]
249249
}
250250
```
251251

252+
It is important to underline that it is only valid to suppress a warning if there are annotations or code that ensure the reflected-on members are visible targets of reflection. It is not sufficient that the member was simply a target of a call, field or property access. It may appear to be the case sometimes but such code is bound to break eventually as more trimming optimizations are added. Properties, fields, and methods that are not visible targets of reflection could be inlined, have their names removed, get moved to different types, or otherwise optimized in ways that will break reflecting on them. When suppressing a warning, it's only permissible to reflect on targets that were visible targets of reflection to the trimming analyzer elsewhere.
253+
254+
```csharp
255+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2063",
256+
// Invalid justification and suppression: property being non-reflectively
257+
// used by the app doesn't guarantee that the property will be available
258+
// for reflection. Properties that are not visible targets of reflection
259+
// are already optimized away with Native AOT trimming and may be
260+
// optimized away for non-native deployment in the future as well.
261+
Justification = "Only need to serialize properties that are used by the app.")]
262+
public string Serialize(object o)
263+
{
264+
StringBuilder sb = new StringBuilder();
265+
foreach (var property in o.GetType.GetProperties())
266+
{
267+
AppendProperty(sb, property, o);
268+
}
269+
return sb.ToString();
270+
}
271+
```
272+
252273
### DynamicDependency
253274

254275
This attribute can be used to indicate that a member has a dynamic dependency on other members. This results in the referenced members being kept whenever the member with the attribute is kept, but doesn't silence warnings on its own. Unlike the other attributes which teach the trim analysis about the reflection behavior of your code, `DynamicDependency` only keeps additional members. This can be used together with `UnconditionalSuppressMessageAttribute` to fix some analysis warnings.

0 commit comments

Comments
 (0)