You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add warning about DAM and add info about DAM on type for IL2075 (#42306)
* Add warning about DAM and add info about DAM on type for IL2075
* Update docs/core/deploying/trimming/trim-warnings/il2075.md
Co-authored-by: Sven Boemer <[email protected]>
---------
Co-authored-by: Sven Boemer <[email protected]>
Copy file name to clipboardExpand all lines: docs/core/deploying/trimming/fixing-warnings.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -222,6 +222,9 @@ Warnings produced by these extra requirements are automatically suppressed if th
222
222
223
223
Unlike `RequiresUnreferencedCode`, which simply reports the incompatibility, adding `DynamicallyAccessedMembers` makes the code compatible with trimming.
224
224
225
+
> [!NOTE]
226
+
> Using `DynamicallyAccessedMembersAttribute` will root all the specified `DynamicallyAccessedMemberTypes` members of the type. This means it will keep the members, as well as any metadata referenced by those members. This can lead to much larger apps than expected. Be careful to use the minimum `DynamicallyAccessedMemberTypes` required.
227
+
225
228
### Suppressing trimmer warnings
226
229
227
230
If you can somehow determine that the call is safe, and all the code that's needed won't be trimmed away, you can also suppress the warning using <xref:System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute>. For example:
Copy file name to clipboardExpand all lines: docs/core/deploying/trimming/trim-warnings/il2075.md
+52-2Lines changed: 52 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,56 @@ void TestMethod()
24
24
}
25
25
```
26
26
27
-
## Fixing
27
+
To solve this issue, add a `DynamicallyAccessedMembersAttribute` to the return of the method that returns the <xref:System.Type> object that you call an annotated instance method on.
28
28
29
-
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for guidance.
// IL2075 Trim analysis: 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to 'GetMethods'. The return value of method 'GetCustomType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
38
+
GetCustomType().GetMethods(); // Type.GetMethods is annotated with DynamicallyAccessedMemberTypes.PublicMethods
39
+
}
40
+
```
41
+
42
+
Another common situation is calling reflection APIs on a <xref:System.Type> object returned by <xref:System.Object.GetType>.
43
+
44
+
```csharp
45
+
voidMyMethod(MyTypeargument)
46
+
{
47
+
Typet=argument.GetType();
48
+
// IL2075 Trim analysis: 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to 'GetMethods'. The return value of method 'Object.GetType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
49
+
t.GetMethods();
50
+
}
51
+
```
52
+
53
+
In this scenario, the solution is to annotate the definition of `MyType` with `DynamicallyAccessedMembersAttribute`.
// IL2075 Trim analysis: 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to 'GetMethods'. The return value of method 'Object.GetType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
68
+
t.GetMethods();
69
+
}
70
+
```
71
+
72
+
Applying `DynamicallyAccessedMembersAttribute` to a class, interface, or struct, indicates to the linker the members specified may be accessed dynamically on <xref:System.Type> instances returned from calling <xref:System.Object.GetType> on instances of that class, interface, or struct.
73
+
74
+
> [!NOTE]
75
+
> Applying `DynamicallyAccessedMembersAttribute` to a type definition will "root" all the indicated `DynamicallyAccessedMemberTypes` on the type and all it's derived types (or implementing types when placed on an interface). This means the members will be kept, as well as any metadata referenced by the members. Be careful to use the minimum `DynamicalylAccessedMemberTypes` required, and apply it on the most specific type possible.
76
+
77
+
## More information
78
+
79
+
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for more information.
0 commit comments