-
Notifications
You must be signed in to change notification settings - Fork 6k
Add missing docs for trim warning codes #42315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
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." | ||
description: "Learn about trim warning IL2071: DynamicallyAccessedMembersMismatchParameterTargetsGenericParameter" | ||
ms.date: 08/23/2024 | ||
author: sbomer | ||
f1_keywords: | ||
- "IL2071" | ||
--- | ||
# 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 | ||
|
||
## Cause | ||
|
||
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. | ||
|
||
## Example | ||
|
||
```csharp | ||
public void GenericWithAnnotation<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>() | ||
{ | ||
} | ||
|
||
void TestMethod(Type type) | ||
{ | ||
// 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. | ||
typeof(AnnotatedGenerics).GetMethod(nameof(GenericWithAnnotation)).MakeGenericMethod(type); | ||
} | ||
``` | ||
|
||
## Fixing | ||
|
||
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for guidance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
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." | ||
description: "Learn about trim warning IL2076: DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsGenericParameter" | ||
ms.date: 08/23/2024 | ||
author: sbomer | ||
f1_keywords: | ||
- "IL2076" | ||
--- | ||
# 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 | ||
|
||
## Cause | ||
|
||
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. | ||
|
||
## Example | ||
|
||
```csharp | ||
public void GenericWithAnnotation<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>() | ||
{ | ||
} | ||
|
||
Type GetType() => typeof(int); | ||
|
||
void TestMethod() | ||
{ | ||
// 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 | ||
typeof(AnnotatedGenerics).GetMethod(nameof(GenericWithAnnotation)).MakeGenericMethod(GetType()); | ||
} | ||
``` | ||
|
||
## Fixing | ||
|
||
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for guidance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
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." | ||
description: "Learn about trim warning IL2081: DynamicallyAccessedMembersMismatchFieldTargetsGenericParameter" | ||
ms.date: 08/23/2024 | ||
author: sbomer | ||
f1_keywords: | ||
- "IL2081" | ||
--- | ||
# 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 | ||
|
||
## Cause | ||
|
||
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. | ||
|
||
## Example | ||
|
||
```csharp | ||
public void GenericWithAnnotation<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>() | ||
{ | ||
} | ||
|
||
Type typeField; | ||
|
||
void TestMethod() | ||
{ | ||
// 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. | ||
typeof(AnnotatedGenerics).GetMethod(nameof(GenericWithAnnotation)).MakeGenericMethod(typeField); | ||
} | ||
``` | ||
|
||
## Fixing | ||
|
||
See [Fixing Warnings](../fixing-warnings.md#functionality-with-requirements-on-its-input) for guidance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: "IL2122: Type 'type' is not assembly qualified. Type name strings used for dynamically accessing a type should be assembly qualified." | ||
description: "Learn about trim warning IL2122: TypeNameIsNotAssemblyQualified" | ||
ms.date: 08/23/2024 | ||
author: sbomer | ||
f1_keywords: | ||
- "IL2122" | ||
--- | ||
# IL2122: Type 'type' is not assembly qualified. Type name strings used for dynamically accessing a type should be assembly qualified | ||
|
||
## Cause | ||
|
||
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. | ||
|
||
## Example | ||
|
||
```csharp | ||
// In Assembly 1 | ||
void TestInvalidTypeName() | ||
{ | ||
// IL2122: Type 'MyType' is not assembly qualified. Type name strings used for dynamically accessing a type should be assembly qualified. | ||
RequirePublicMethodOnAType("MyType"); | ||
} | ||
|
||
void ForwardTypeNameToAnotherMethod( | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] | ||
string typeName) | ||
{ | ||
MyTypeFromAnotherAssembly.GetType(typeName); | ||
} | ||
``` | ||
|
||
```csharp | ||
// In Assembly 2 | ||
public class MyTypeFromAnotherAssembly | ||
{ | ||
void GetTypeAndSearchThroughMethods( | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] | ||
string typeName) | ||
{ | ||
Type.GetType(typeName).GetMethods(); | ||
} | ||
} | ||
|
||
class MyType | ||
{ | ||
// ... | ||
} | ||
``` | ||
|
||
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. | ||
|
||
To fix this, consider using a fully-qualified type name instead: | ||
|
||
```csharp | ||
RequirePublicMethodsOnAType("MyType,Assembly2"); | ||
``` | ||
|
||
Another option is to pass the unqualified type name string directly to `Type.GetType`, and avoid annotations on `string`: | ||
|
||
```csharp | ||
var type = Type.GetType("MyType"); | ||
|
||
void SearchThroughMethods( | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] | ||
Type type) | ||
{ | ||
type.GetMethods(); | ||
} | ||
``` | ||
|
||
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.