Skip to content

Commit 3a5c16b

Browse files
jtschustersbomer
andauthored
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]>
1 parent e373aa6 commit 3a5c16b

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

docs/core/deploying/trimming/fixing-warnings.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ Warnings produced by these extra requirements are automatically suppressed if th
222222

223223
Unlike `RequiresUnreferencedCode`, which simply reports the incompatibility, adding `DynamicallyAccessedMembers` makes the code compatible with trimming.
224224

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+
225228
### Suppressing trimmer warnings
226229

227230
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:

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,56 @@ void TestMethod()
2424
}
2525
```
2626

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.
2828

29-
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for guidance.
29+
```csharp
30+
using System.Diagnostics.CodeAnalysis;
31+
32+
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
33+
Type GetCustomType() { return typeof(CustomType); }
34+
35+
void TestMethod()
36+
{
37+
// 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+
void MyMethod(MyType argument)
46+
{
47+
Type t = 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`.
54+
55+
```csharp
56+
using System.Diagnostics.CodeAnalysis;
57+
58+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
59+
class MyType
60+
{
61+
...
62+
}
63+
64+
void MyMethod(MyType argument)
65+
{
66+
Type t = argument.GetType();
67+
// 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

Comments
 (0)