Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/core/deploying/trimming/trim-warnings/il2111.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The trimmer can't guarantee that all requirements of the <xref:System.Diagnostic

## Example

This warning can be caused by directly accessing a method with a <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute> on its parameters or return type.

```csharp
void MethodWithRequirements([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type)
{
Expand All @@ -25,3 +27,27 @@ void TestMethod()
typeof(Test).GetMethod("MethodWithRequirements");
}
```

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.

```csharp
class TypeWithAnnotatedMethod
{
void MethodWithRequirements([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type type)
{
}
}

class OtherType
{
void AccessMethodViaReflection([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type)
{
}

void PassTypeToAnnotatedMethod()
{
// 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.
AccessMethodViaReflection(typeof(TypeWithAnnotatedMethod));
}
}
```