Skip to content

Commit a4fc471

Browse files
authored
Add example for the more common and confusing IL2111 cases (#40789)
* Update il2111.md Document confusing example of IL2111 warning. * Update il2111.md * Remove extra lines
1 parent f18fe13 commit a4fc471

File tree

1 file changed

+26
-0
lines changed
  • docs/core/deploying/trimming/trim-warnings

1 file changed

+26
-0
lines changed

docs/core/deploying/trimming/trim-warnings/il2111.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ The trimmer can't guarantee that all requirements of the <xref:System.Diagnostic
1414

1515
## Example
1616

17+
This warning can be caused by directly accessing a method with a <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute> on its parameters or return type.
18+
1719
```csharp
1820
void MethodWithRequirements([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type)
1921
{
@@ -25,3 +27,27 @@ void TestMethod()
2527
typeof(Test).GetMethod("MethodWithRequirements");
2628
}
2729
```
30+
31+
This warning can also be caused by passing a type to a field, paramter, argument, or return value that is annotated with <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>. <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute> implies reflection access over all of the listed <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes>. This means that when a type is passed to a parameter, field, generic parameter, or return value annotated with <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods> .NET tooling assumes that all public methods are accessed via reflection. If a type that contains a method with an annotated parameter or return value is passed to a location annotated with <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods>, then IL2111 will be raised.
32+
33+
```csharp
34+
class TypeWithAnnotatedMethod
35+
{
36+
void MethodWithRequirements([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type type)
37+
{
38+
}
39+
}
40+
41+
class OtherType
42+
{
43+
void AccessMethodViaReflection([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type)
44+
{
45+
}
46+
47+
void PassTypeToAnnotatedMethod()
48+
{
49+
// IL2111: Method 'MethodWithRequirements' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method.
50+
AccessMethodViaReflection(typeof(TypeWithAnnotatedMethod));
51+
}
52+
}
53+
```

0 commit comments

Comments
 (0)