Handling TypeForwardedFromAttribute while trimming
- Dominant language
- C#
- Stars
- 392
- Forks
- 128
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 2
Description
The [TypeForwardedFromAttribute](https://github.com/dotnet/runtime/blob/c8e3d8c461b5a7033513533af411f55f1c103c05/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/TypeForwardedFromAttribute.cs#L7) allows for serializers (i.e. [BinaryFormatter](https://github.com/dotnet/runtime/blob/8a52f1e948b6f22f418817ec1068f07b8dae2aa5/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/FormatterServices.cs#L335-L339) and [XmlSerializer](https://github.com/dotnet/runtime/blob/8a52f1e948b6f22f418817ec1068f07b8dae2aa5/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs#L66-L72)) to write Type names using a "stable" assembly name - one that won't change from version-to-version even if the Type moves assemblies.
These attributes typically point to facade assemblies.
The issue is that trimming will remove the facade assemblies. Thus code in [XmlSerializer](https://github.com/dotnet/runtime/blob/8a52f1e948b6f22f418817ec1068f07b8dae2aa5/src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs#L66-L72), which tries loading that assembly, is going to fail.
If the trimmer sees a `TypeForwardedFromAttribute` to a facade assembly, it is going to need to preserve that facade assembly after trimming.
I tried to find other attributes that have this same issue, and came up with 2 other instances of attributes taking assembly names:
1. InternalsVisibleToAttribute
2. DynamicDependencyAttribute
We may need to handle `DynamicDependency` similar to `TypeForwardedFrom` - if the attribute is referencing an assembly, that assembly can't be trimmed. My initial thoughts for `InternalsVisibleTo` is that it shouldn't matter if the assembly is trimmed.
If this pattern exists for more places than these attributes, we may have to add a new annotation that tells the trimmer the string is an "assembly name". Similarly to how `[DynamicallyAccessedMembersAttribute]` on a string tells the trimmer the string is a Type name.
cc @ericstj @safern
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.