-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Update trimming doc #36487
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
Update trimming doc #36487
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
f745fc9
Update trimming doc
Rick-Anderson 2298407
Update trimming doc
Rick-Anderson 6cd4bc9
Update trimming doc
Rick-Anderson 2a6ab04
Update trimming doc
Rick-Anderson fceae61
Update trimming doc
Rick-Anderson 74e8cc3
Update trimming doc
Rick-Anderson 245066e
Update trimming doc
Rick-Anderson 6de398e
Update trimming doc
Rick-Anderson 3a4c3c2
Update trimming doc
Rick-Anderson 2276a52
Update trimming doc
Rick-Anderson 75a2fdd
Update trimming doc
Rick-Anderson bb1f7ce
Update trimming doc
Rick-Anderson bdc17f1
Update trimming doc
Rick-Anderson 8c768f6
Update trimming doc
Rick-Anderson 97a5657
Update trimming doc
Rick-Anderson ba9b9f7
Update trimming doc
Rick-Anderson c4e63d5
Update trimming doc
Rick-Anderson ea48fd5
Update trimming doc
Rick-Anderson aa0429e
Update trimming doc
Rick-Anderson 26ff3cb
Update trimming doc
Rick-Anderson 6ba2a9e
Update trimming doc
Rick-Anderson 440bf8a
Update trimming doc
Rick-Anderson 5e8ee9b
Update trimming doc
Rick-Anderson 14a35fa
Update trimming doc
Rick-Anderson e0cc309
Update trimming doc
Rick-Anderson 6a3f859
Update trimming doc
Rick-Anderson 56aef48
Update trimming doc
Rick-Anderson 9af16a9
Apply suggestions from code review
Rick-Anderson f224ea9
react to feedback
Rick-Anderson 9c2766a
react to feedback
Rick-Anderson 08bb4a7
react to feedback
Rick-Anderson f77702f
react to feedback
Rick-Anderson 9cce0c1
react to feedback
Rick-Anderson 19babff
Apply suggestions from code review
Rick-Anderson 46f7dfa
react to feedback
Rick-Anderson e916012
react to feedback
Rick-Anderson f6e29ec
Apply suggestions from code review
Rick-Anderson ef61a9b
react to feedback
Rick-Anderson 7c74672
react to feedback
Rick-Anderson 77abc93
react to feedback
Rick-Anderson f1a2fbc
react to feedback
Rick-Anderson 0f60845
react to feedback
Rick-Anderson 9726db2
react to feedback
Rick-Anderson 24c58e5
Apply suggestions from code review
Rick-Anderson 71b0617
react to feedback
Rick-Anderson d6daddf
react to feedback
Rick-Anderson 0c58ab6
react to feedback
Rick-Anderson 1830a37
react to feedback
Rick-Anderson b6c86c2
react to feedback
Rick-Anderson 8436c11
react to feedback
Rick-Anderson b8bec1a
react to feedback
Rick-Anderson 41202fb
react to feedback
Rick-Anderson 59a533a
Apply suggestions from code review
Rick-Anderson 96c45bc
react to feedback
Rick-Anderson e765adb
react to feedback
Rick-Anderson 1608032
react to feedback
Rick-Anderson 45041e1
react to feedback
Rick-Anderson 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
424 changes: 161 additions & 263 deletions
424
docs/core/deploying/trimming/prepare-libraries-for-trimming.md
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
docs/core/deploying/trimming/snippets/ConsoleApp1/ConsoleApp1.csproj
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,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <PublishTrimmed>true</PublishTrimmed> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\MyLibrary\MyLibrary.csproj" /> | ||
| <TrimmerRootAssembly Include="MyLibrary" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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,4 @@ | ||
| using System; | ||
| using MyLibrary1; | ||
|
|
||
| Console.WriteLine(MyLib.MyClass.getMax(1, 2)); |
220 changes: 220 additions & 0 deletions
220
docs/core/deploying/trimming/snippets/MyLibrary/Class1.cs
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,220 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Reflection; | ||
| using System.Text; | ||
|
|
||
| namespace MyLibrary1; | ||
|
|
||
| // <snippet_1> | ||
| public class MyLibrary | ||
| { | ||
| public static void MyMethod() | ||
| { | ||
| // warning IL2026 : | ||
| // MyLibrary.MyMethod: Using 'MyLibrary.DynamicBehavior' | ||
| // which has [RequiresUnreferencedCode] can break functionality | ||
| // when trimming app code. | ||
| DynamicBehavior(); | ||
| } | ||
|
|
||
| [RequiresUnreferencedCode( | ||
| "DynamicBehavior is incompatible with trimming.")] | ||
| static void DynamicBehavior() | ||
| { | ||
| } | ||
| } | ||
| // </snippet_1> | ||
|
|
||
| // <snippet_DAA1> | ||
| public class MyLibrary3 | ||
| { | ||
| static void UseMethods(Type type) | ||
| { | ||
| // warning IL2070: MyLibrary.UseMethods(Type): 'this' argument does not satisfy | ||
| // 'DynamicallyAccessedMemberTypes.PublicMethods' in call to | ||
| // 'System.Type.GetMethods()'. | ||
| // The parameter 't' of method 'MyLibrary.UseMethods(Type)' doesn't have | ||
| // matching annotations. | ||
| foreach (var method in type.GetMethods()) | ||
| { | ||
| // ... | ||
| } | ||
| } | ||
| } | ||
| // </snippet_DAA1> | ||
|
|
||
| public class MyLibrary4 | ||
| { | ||
| // <snippet_DAA2> | ||
| static void UseMethods( | ||
| // State the requirement in the UseMethods parameter. | ||
| [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type) | ||
| { | ||
| // ... | ||
| } | ||
| // </snippet_DAA2> | ||
| } | ||
|
|
||
| public class MyLibrary5 | ||
| { | ||
| private static void UseMethods(object type) => throw new NotImplementedException(); | ||
|
|
||
| // <snippet_UMH> | ||
| static Type type; | ||
| static void UseMethodsHelper() | ||
| { | ||
| // warning IL2077: MyLibrary.UseMethodsHelper(Type): 'type' argument does not satisfy | ||
| // 'DynamicallyAccessedMemberTypes.PublicMethods' in call to | ||
| // 'MyLibrary.UseMethods(Type)'. | ||
| // The field 'System.Type MyLibrary::type' does not have matching annotations. | ||
| UseMethods(type); | ||
| } | ||
| // </snippet_UMH> | ||
| } | ||
|
|
||
|
|
||
| public class MyLibrary7 | ||
| { | ||
| // <snippet_AD1> | ||
| class TypeCollection | ||
| { | ||
| Type[] types; | ||
|
|
||
| // Ensure that only types with preserved constructors are stored in the array | ||
| [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] | ||
| public Type this[int i] | ||
| { | ||
| // warning IL2063: TypeCollection.Item.get: Value returned from method | ||
| // 'TypeCollection.Item.get' can't be statically determined and may not meet | ||
| // 'DynamicallyAccessedMembersAttribute' requirements. | ||
| get => types[i]; | ||
| set => types[i] = value; | ||
| } | ||
| } | ||
|
|
||
| class TypeCreator | ||
| { | ||
| TypeCollection types; | ||
|
|
||
| public void CreateType(int i) | ||
| { | ||
| types[i] = typeof(TypeWithConstructor); | ||
| Activator.CreateInstance(types[i]); // No warning! | ||
| } | ||
| } | ||
|
|
||
| class TypeWithConstructor | ||
| { | ||
| } | ||
| // </snippet_AD1> | ||
| } | ||
|
|
||
| public class MyLibrary8 | ||
| { | ||
| // <snippet_AD2> | ||
| class TypeCollection | ||
| { | ||
| Type[] types; | ||
|
|
||
| // Ensure that only types with preserved constructors are stored in the array | ||
| [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] | ||
| public Type this[int i] | ||
| { | ||
| [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2063", | ||
| Justification = "The list only contains types stored through the annotated setter.")] | ||
| get => types[i]; | ||
| set => types[i] = value; | ||
| } | ||
| } | ||
|
|
||
| class TypeCreator | ||
| { | ||
| TypeCollection types; | ||
|
|
||
| public void CreateType(int i) | ||
| { | ||
| types[i] = typeof(TypeWithConstructor); | ||
| Activator.CreateInstance(types[i]); // No warning! | ||
| } | ||
| } | ||
|
|
||
| class TypeWithConstructor | ||
| { | ||
| } | ||
| // </snippet_AD2> | ||
| } | ||
|
|
||
| public class MyLibrary11 | ||
| { | ||
| // <snippet_AD3> | ||
| // Invalid justification and suppression: property being non-reflectively | ||
| // used by the app doesn't guarantee that the property will be available | ||
| // for reflection. Properties that are not visible targets of reflection | ||
| // are already optimized away with Native AOT trimming and may be | ||
| // optimized away for non-native deployment in the future as well. | ||
| [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2063", | ||
| Justification = "*INVALID* Only need to serialize properties that are used by" | ||
| + "the app. *INVALID*")] | ||
| public string Serialize(object o) | ||
| { | ||
| StringBuilder sb = new StringBuilder(); | ||
| foreach (var property in o.GetType().GetProperties()) | ||
| { | ||
| AppendProperty(sb, property, o); | ||
| } | ||
| return sb.ToString(); | ||
| } | ||
| // </snippet_AD3> | ||
| private void AppendProperty(StringBuilder sb, PropertyInfo property, object o) => throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public class MyLibrary12 | ||
| { | ||
| // <snippet_AD4> | ||
| [DynamicDependency("Helper", "MyType", "MyAssembly")] | ||
| static void RunHelper() | ||
| { | ||
| var helper = Assembly.Load("MyAssembly").GetType("MyType").GetMethod("Helper"); | ||
| helper.Invoke(null, null); | ||
| } | ||
| // </snippet_AD4> | ||
| } | ||
|
|
||
| public class MyLib | ||
| { | ||
| public static class MyClass | ||
| { | ||
| public static int getMax(int a, int b) | ||
| { | ||
| return a > b ? a : b; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| public class MyLibrary99 | ||
| { | ||
| internal static Type type; | ||
| } | ||
| public class MyLibrary22 | ||
| { | ||
| // <snippet_AD5> | ||
| [DynamicDependency("MyMethod()")] | ||
| [DynamicDependency("MyMethod(System,Boolean,System.String)")] | ||
| [DynamicDependency("MethodOnDifferentType()", typeof(ContainingType))] | ||
| [DynamicDependency("MemberName")] | ||
| [DynamicDependency("MemberOnUnreferencedAssembly", "ContainingType" | ||
| , "UnreferencedAssembly")] | ||
| [DynamicDependency("MemberName", "Namespace.ContainingType.NestedType", "Assembly")] | ||
| // generics | ||
| [DynamicDependency("GenericMethodName``1")] | ||
| [DynamicDependency("GenericMethod``2(``0,``1)")] | ||
| [DynamicDependency( | ||
| "MethodWithGenericParameterTypes(System.Collections.Generic.List{System.String})")] | ||
| [DynamicDependency("MethodOnGenericType(`0)", "GenericType`1", "UnreferencedAssembly")] | ||
| [DynamicDependency("MethodOnGenericType(`0)", typeof(GenericType<>))] | ||
| // </snippet_AD5> | ||
| static void RunHelper() | ||
| { | ||
| var helper = Assembly.Load("MyAssembly").GetType("MyType").GetMethod("Helper"); | ||
| helper.Invoke(null, null); | ||
| } | ||
| } | ||
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,22 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace MyLibrary2; | ||
| class Class2 | ||
| { | ||
| // <snippet_RequiresUnreferencedCode> | ||
| public class MyLibrary | ||
| { | ||
| [RequiresUnreferencedCode("Calls DynamicBehavior.")] | ||
| public static void MyMethod() | ||
| { | ||
| DynamicBehavior(); | ||
| } | ||
|
|
||
| [RequiresUnreferencedCode( | ||
| "DynamicBehavior is incompatible with trimming.")] | ||
| static void DynamicBehavior() | ||
| { | ||
| } | ||
| } | ||
| // </snippet_RequiresUnreferencedCode> | ||
| } |
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,36 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace MyLibrary3; | ||
| class Class3 | ||
| { | ||
| // <snippet_1> | ||
| public class MyLibrary | ||
| { | ||
| internal static Type type; | ||
|
|
||
| [RequiresUnreferencedCode("Calls DynamicBehavior.")] | ||
| public static void MyMethod() | ||
| { | ||
| DynamicBehavior(); | ||
| } | ||
|
|
||
| [RequiresUnreferencedCode( | ||
| "DynamicBehavior is incompatible with trimming.")] | ||
| static void DynamicBehavior() | ||
| { | ||
| } | ||
| } | ||
|
|
||
| public class MyLibrary6 | ||
| { | ||
| // <snippet_UMH2> | ||
| [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] | ||
| static Type type; | ||
|
|
||
| static void UseMethodsHelper() | ||
| { | ||
| MyLibrary.type = typeof(System.Tuple); | ||
| } | ||
| // </snippet_UMH2> | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
docs/core/deploying/trimming/snippets/MyLibrary/ContainingType.cs
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,3 @@ | ||
| internal class ContainingType | ||
| { | ||
| } |
3 changes: 3 additions & 0 deletions
3
docs/core/deploying/trimming/snippets/MyLibrary/GenericType.cs
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,3 @@ | ||
| internal class GenericType<T> | ||
| { | ||
| } |
8 changes: 8 additions & 0 deletions
8
docs/core/deploying/trimming/snippets/MyLibrary/MyLibrary.csproj
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,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
17 changes: 17 additions & 0 deletions
17
docs/core/deploying/trimming/snippets/MyLibrary/MyLibrary.csproj.xml
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,17 @@ | ||
| <!-- <snippet_full> --> | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
Rick-Anderson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <!-- <snippet> --> | ||
Rick-Anderson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <PropertyGroup> | ||
| <IsTrimmable>true</IsTrimmable> | ||
| </PropertyGroup> | ||
| <!-- </snippet> --> | ||
|
|
||
| </Project> | ||
| <!-- </snippet_full> --> | ||
11 changes: 11 additions & 0 deletions
11
docs/core/deploying/trimming/snippets/MyTestLib6app/MyTestLib6app.csproj
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,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <PublishTrimmed>true</PublishTrimmed> | ||
| <!-- Prevent warnings from unused code in dependencies --> | ||
| <TrimmerDefaultAction>link</TrimmerDefaultAction> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
2 changes: 2 additions & 0 deletions
2
docs/core/deploying/trimming/snippets/MyTestLib6app/Program.cs
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,2 @@ | ||
| // See https://aka.ms/new-console-template for more information | ||
| System.Console.WriteLine("Hello, World!"); |
17 changes: 17 additions & 0 deletions
17
docs/core/deploying/trimming/snippets/MyTestLib6app/XMLFile1.xml
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,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <PublishTrimmed>true</PublishTrimmed> | ||
| <!-- Prevent warnings from unused code in dependencies --> | ||
| <TrimmerDefaultAction>link</TrimmerDefaultAction> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="path/to/MyLibrary.csproj" /> | ||
| <!-- Analyze the whole library, even if attributed with "IsTrimmable" --> | ||
| <TrimmerRootAssembly Include="MyLibrary" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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.