Skip to content
Merged
Show file tree
Hide file tree
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 Aug 2, 2023
2298407
Update trimming doc
Rick-Anderson Aug 2, 2023
6cd4bc9
Update trimming doc
Rick-Anderson Aug 2, 2023
2a6ab04
Update trimming doc
Rick-Anderson Aug 2, 2023
fceae61
Update trimming doc
Rick-Anderson Aug 2, 2023
74e8cc3
Update trimming doc
Rick-Anderson Aug 2, 2023
245066e
Update trimming doc
Rick-Anderson Aug 2, 2023
6de398e
Update trimming doc
Rick-Anderson Aug 2, 2023
3a4c3c2
Update trimming doc
Rick-Anderson Aug 3, 2023
2276a52
Update trimming doc
Rick-Anderson Aug 3, 2023
75a2fdd
Update trimming doc
Rick-Anderson Aug 3, 2023
bb1f7ce
Update trimming doc
Rick-Anderson Aug 3, 2023
bdc17f1
Update trimming doc
Rick-Anderson Aug 3, 2023
8c768f6
Update trimming doc
Rick-Anderson Aug 3, 2023
97a5657
Update trimming doc
Rick-Anderson Aug 3, 2023
ba9b9f7
Update trimming doc
Rick-Anderson Aug 3, 2023
c4e63d5
Update trimming doc
Rick-Anderson Aug 3, 2023
ea48fd5
Update trimming doc
Rick-Anderson Aug 4, 2023
aa0429e
Update trimming doc
Rick-Anderson Aug 4, 2023
26ff3cb
Update trimming doc
Rick-Anderson Aug 4, 2023
6ba2a9e
Update trimming doc
Rick-Anderson Aug 4, 2023
440bf8a
Update trimming doc
Rick-Anderson Aug 4, 2023
5e8ee9b
Update trimming doc
Rick-Anderson Aug 4, 2023
14a35fa
Update trimming doc
Rick-Anderson Aug 5, 2023
e0cc309
Update trimming doc
Rick-Anderson Aug 5, 2023
6a3f859
Update trimming doc
Rick-Anderson Aug 8, 2023
56aef48
Update trimming doc
Rick-Anderson Aug 8, 2023
9af16a9
Apply suggestions from code review
Rick-Anderson Aug 8, 2023
f224ea9
react to feedback
Rick-Anderson Aug 8, 2023
9c2766a
react to feedback
Rick-Anderson Aug 8, 2023
08bb4a7
react to feedback
Rick-Anderson Aug 8, 2023
f77702f
react to feedback
Rick-Anderson Aug 9, 2023
9cce0c1
react to feedback
Rick-Anderson Aug 9, 2023
19babff
Apply suggestions from code review
Rick-Anderson Aug 16, 2023
46f7dfa
react to feedback
Rick-Anderson Aug 19, 2023
e916012
react to feedback
Rick-Anderson Aug 19, 2023
f6e29ec
Apply suggestions from code review
Rick-Anderson Aug 22, 2023
ef61a9b
react to feedback
Rick-Anderson Aug 22, 2023
7c74672
react to feedback
Rick-Anderson Aug 22, 2023
77abc93
react to feedback
Rick-Anderson Aug 22, 2023
f1a2fbc
react to feedback
Rick-Anderson Aug 22, 2023
0f60845
react to feedback
Rick-Anderson Aug 23, 2023
9726db2
react to feedback
Rick-Anderson Aug 23, 2023
24c58e5
Apply suggestions from code review
Rick-Anderson Aug 23, 2023
71b0617
react to feedback
Rick-Anderson Aug 23, 2023
d6daddf
react to feedback
Rick-Anderson Aug 23, 2023
0c58ab6
react to feedback
Rick-Anderson Aug 23, 2023
1830a37
react to feedback
Rick-Anderson Aug 23, 2023
b6c86c2
react to feedback
Rick-Anderson Aug 23, 2023
8436c11
react to feedback
Rick-Anderson Aug 23, 2023
b8bec1a
react to feedback
Rick-Anderson Aug 24, 2023
41202fb
react to feedback
Rick-Anderson Aug 24, 2023
59a533a
Apply suggestions from code review
Rick-Anderson Aug 24, 2023
96c45bc
react to feedback
Rick-Anderson Aug 24, 2023
e765adb
react to feedback
Rick-Anderson Aug 24, 2023
1608032
react to feedback
Rick-Anderson Aug 24, 2023
45041e1
react to feedback
Rick-Anderson Aug 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
424 changes: 161 additions & 263 deletions docs/core/deploying/trimming/prepare-libraries-for-trimming.md

Large diffs are not rendered by default.

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>
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 docs/core/deploying/trimming/snippets/MyLibrary/Class1.cs
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);
}
}
22 changes: 22 additions & 0 deletions docs/core/deploying/trimming/snippets/MyLibrary/Class2.cs
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>
}
36 changes: 36 additions & 0 deletions docs/core/deploying/trimming/snippets/MyLibrary/Class3.cs
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>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
internal class ContainingType
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
internal class GenericType<T>
{
}
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>
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>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<!-- <snippet> -->
<PropertyGroup>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
<!-- </snippet> -->

</Project>
<!-- </snippet_full> -->
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>
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!");
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>