Skip to content

Commit 8bc010a

Browse files
authored
Add missing docs for trim warning codes (#42315)
* Add missing docs for trim warnings * PR feedback - Improve example for IL2122 - 'target method or type' * Fix warnings * Fix warnings
1 parent a876aac commit 8bc010a

File tree

6 files changed

+179
-6
lines changed

6 files changed

+179
-6
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@ The target location declares some requirements on the type value via its <xref:S
1515
## Example
1616

1717
```csharp
18-
void GenericWithAnnotation<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>()
19-
{
20-
}
21-
2218
void TestMethod(Type type)
2319
{
24-
// IL2070 Trim analysis: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])'. The parameter 'type' of method 'TestMethod' 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.
25-
typeof(AnnotatedGenerics).GetMethod(nameof(GenericWithAnnotation)).MakeGenericMethod(type);
20+
// IL2070 Trim analysis: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The parameter 'type' of method 'TestMethod(Type)' 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.
21+
type.GetMethod("Method");
2622
}
2723
```
2824

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: "IL2071: 'target generic parameter' generic argument does not satisfy 'DynamicallyAccessedMembersAttribute' in 'target method or type'. The parameter 'source parameter' of method 'source method' 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."
3+
description: "Learn about trim warning IL2071: DynamicallyAccessedMembersMismatchParameterTargetsGenericParameter"
4+
ms.date: 08/23/2024
5+
author: sbomer
6+
f1_keywords:
7+
- "IL2071"
8+
---
9+
# IL2071: 'target generic parameter' generic argument does not satisfy 'DynamicallyAccessedMembersAttribute' in 'target method or type'. The parameter 'source parameter' of method 'source method' 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
10+
11+
## Cause
12+
13+
The target location declares some requirements on the type value via its <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>. Those requirements must be met by those declared on the source value also via the <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>. The source value can declare more requirements than the target, if necessary.
14+
15+
## Example
16+
17+
```csharp
18+
public void GenericWithAnnotation<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>()
19+
{
20+
}
21+
22+
void TestMethod(Type type)
23+
{
24+
// IL2071 Trim Analysis: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in 'GenericWithAnnotation<T>()'. The parameter 'type' of method 'TestMethod(Type)' 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.
25+
typeof(AnnotatedGenerics).GetMethod(nameof(GenericWithAnnotation)).MakeGenericMethod(type);
26+
}
27+
```
28+
29+
## Fixing
30+
31+
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for guidance.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: "IL2076: 'target generic parameter' generic argument does not satisfy 'DynamicallyAccessedMembersAttribute' in 'target method or type'. The return value of method 'source method' 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."
3+
description: "Learn about trim warning IL2076: DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsGenericParameter"
4+
ms.date: 08/23/2024
5+
author: sbomer
6+
f1_keywords:
7+
- "IL2076"
8+
---
9+
# IL2076: 'target generic parameter' generic argument does not satisfy 'DynamicallyAccessedMembersAttribute' in 'target method or type'. The return value of method 'source method' 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
10+
11+
## Cause
12+
13+
The target location declares some requirements on the type value via its <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>. Those requirements must be met by those declared on the source value also via the <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>. The source value can declare more requirements than the target, if necessary.
14+
15+
## Example
16+
17+
```csharp
18+
public void GenericWithAnnotation<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>()
19+
{
20+
}
21+
22+
Type GetType() => typeof(int);
23+
24+
void TestMethod()
25+
{
26+
// IL2076 Trim Analysis: AnnotatedGenerics.TestMethod(Type): 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in 'GenericWithAnnotation<T>()'. The return value of method '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
27+
typeof(AnnotatedGenerics).GetMethod(nameof(GenericWithAnnotation)).MakeGenericMethod(GetType());
28+
}
29+
```
30+
31+
## Fixing
32+
33+
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for guidance.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: "IL2081: 'target generic parameter' generic argument does not satisfy 'DynamicallyAccessedMembersAttribute' in 'target method or type'. The field 'source field' 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."
3+
description: "Learn about trim warning IL2081: DynamicallyAccessedMembersMismatchFieldTargetsGenericParameter"
4+
ms.date: 08/23/2024
5+
author: sbomer
6+
f1_keywords:
7+
- "IL2081"
8+
---
9+
# IL2081: 'target generic parameter' generic argument does not satisfy 'DynamicallyAccessedMembersAttribute' in 'target method or type'. The field 'source field' 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
10+
11+
## Cause
12+
13+
The target location declares some requirements on the type value via its <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>. Those requirements must be met by those declared on the source value also via the <xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>. The source value can declare more requirements than the target, if necessary.
14+
15+
## Example
16+
17+
```csharp
18+
public void GenericWithAnnotation<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>()
19+
{
20+
}
21+
22+
Type typeField;
23+
24+
void TestMethod()
25+
{
26+
// IL2081 Trim Analysis: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in 'GenericWithAnnotation<T>()'. The field 'typeField' 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.
27+
typeof(AnnotatedGenerics).GetMethod(nameof(GenericWithAnnotation)).MakeGenericMethod(typeField);
28+
}
29+
```
30+
31+
## Fixing
32+
33+
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for guidance.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: "IL2122: Type 'type' is not assembly qualified. Type name strings used for dynamically accessing a type should be assembly qualified."
3+
description: "Learn about trim warning IL2122: TypeNameIsNotAssemblyQualified"
4+
ms.date: 08/23/2024
5+
author: sbomer
6+
f1_keywords:
7+
- "IL2122"
8+
---
9+
# IL2122: Type 'type' is not assembly qualified. Type name strings used for dynamically accessing a type should be assembly qualified
10+
11+
## Cause
12+
13+
Type name strings representing dynamically accessed types must be assembly qualified. Otherwise, the lookup semantics of `Type.GetType` will search the assembly with the `Type.GetType` callsite and the core library. The assembly with the `Type.GetType` callsite may be different than the assembly which passes the type name string to a location with `DynamicallyAccessedMembersAttribute`, so the tooling cannot determine which assemblies to search.
14+
15+
## Example
16+
17+
```csharp
18+
// In Assembly 1
19+
void TestInvalidTypeName()
20+
{
21+
// IL2122: Type 'MyType' is not assembly qualified. Type name strings used for dynamically accessing a type should be assembly qualified.
22+
RequirePublicMethodOnAType("MyType");
23+
}
24+
25+
void ForwardTypeNameToAnotherMethod(
26+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
27+
string typeName)
28+
{
29+
MyTypeFromAnotherAssembly.GetType(typeName);
30+
}
31+
```
32+
33+
```csharp
34+
// In Assembly 2
35+
public class MyTypeFromAnotherAssembly
36+
{
37+
void GetTypeAndSearchThroughMethods(
38+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
39+
string typeName)
40+
{
41+
Type.GetType(typeName).GetMethods();
42+
}
43+
}
44+
45+
class MyType
46+
{
47+
// ...
48+
}
49+
```
50+
51+
In a non-trimmed app, at runtime the `Type.GetType` call will discover `MyType` in Assembly 2. However, trimming will remove `MyType` because the trimming tools don't have enough information to determine where the type will be found at runtime.
52+
53+
To fix this, consider using a fully-qualified type name instead:
54+
55+
```csharp
56+
RequirePublicMethodsOnAType("MyType,Assembly2");
57+
```
58+
59+
Another option is to pass the unqualified type name string directly to `Type.GetType`, and avoid annotations on `string`:
60+
61+
```csharp
62+
var type = Type.GetType("MyType");
63+
64+
void SearchThroughMethods(
65+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
66+
Type type)
67+
{
68+
type.GetMethods();
69+
}
70+
```
71+
72+
This gives the trimming tools enough information to look up the type using the same semantics that <xref:System.Type.GetType(System.String)> has at runtime, causing the type (and public methods in this example) to be preserved.

docs/navigate/devops-testing/toc.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ items:
381381
href: ../../core/deploying/trimming/trim-warnings/il2069.md
382382
- name: IL2070
383383
href: ../../core/deploying/trimming/trim-warnings/il2070.md
384+
- name: IL2071
385+
href: ../../core/deploying/trimming/trim-warnings/il2071.md
384386
- name: IL2072
385387
href: ../../core/deploying/trimming/trim-warnings/il2072.md
386388
- name: IL2073
@@ -389,6 +391,8 @@ items:
389391
href: ../../core/deploying/trimming/trim-warnings/il2074.md
390392
- name: IL2075
391393
href: ../../core/deploying/trimming/trim-warnings/il2075.md
394+
- name: IL2076
395+
href: ../../core/deploying/trimming/trim-warnings/il2076.md
392396
- name: IL2077
393397
href: ../../core/deploying/trimming/trim-warnings/il2077.md
394398
- name: IL2078
@@ -397,6 +401,8 @@ items:
397401
href: ../../core/deploying/trimming/trim-warnings/il2079.md
398402
- name: IL2080
399403
href: ../../core/deploying/trimming/trim-warnings/il2080.md
404+
- name: IL2081
405+
href: ../../core/deploying/trimming/trim-warnings/il2081.md
400406
- name: IL2082
401407
href: ../../core/deploying/trimming/trim-warnings/il2082.md
402408
- name: IL2083
@@ -467,6 +473,8 @@ items:
467473
href: ../../core/deploying/trimming/trim-warnings/il2116.md
468474
- name: IL2117
469475
href: ../../core/deploying/trimming/trim-warnings/il2117.md
476+
- name: IL2122
477+
href: ../../core/deploying/trimming/trim-warnings/il2122.md
470478
- name: Native AOT deployment model
471479
items:
472480
- name: Overview

0 commit comments

Comments
 (0)