Repository navigation
Description
Background and Motivation
[See this issue and specifically the conversation from here to get more context]. The API enhancement proposal #49465 will work well with this proposal to provide validation and enforcement to a problematic coding pattern for the trimmer to reason about correctly as described below.
There are many scenarios in .NET where code uses a type in a polymorphic manner that makes it harder for the trimmer, as a static analysis tool, to determine the exact type. This is very common where an interface or a base class type is used in the code to refer to a derived type. A helper function similar to IsAssignableTo but one that returns a type will help the trimmer to apply parent type annotation to the full inheritance graph of the parent type. This method can be added as an extension method in the System.Diagnostic.CodeAnalysis namespace with other Trimmer helper types.
Proposed API
namespace System.Diagnostics.CodeAnalysis
{
+ public static class TypeExensions {
+ public static Type AssumeAssignableTo(this Type type, Type? targetType) {
}
}The API should be similar to Type.IsAssignable except return the same type if assignable to targetType or throw otherwise
Usage Examples
This API will be used by developers who want to trim their applications but need to use problematic code patterns that are difficult for the trimmer to reason about at trim time. By using this API, the trimmer will preserve all methods of all derived types of the targetType.
// New code
Type someType = GetTypeFromSomewhere().AssumeAssignableTo(typeof(EventSource));
// AssumeAssignableTo validated at runtime that the type passed to it implements the base type or an interface
// It also now allows the trimmer to validate that the type in question implements the base type or an interface
// Trimmer sees we're calling GetMethods on something that derives from EventSource. It needs to
// go over all EventSource-implementing types that were marked and keep their methods.
foreach (var method in someType.GetMethods())
{
// ...
}
Alternative Designs
Option 1: Use the existing Type API, IsAssignableTo : The trimmer does not track branch level analysis for types, and even with such support, the usage scenario is going to be very challenging.
Option 2: Proposal #49465 on its own will help solve a subset of the problem
Option 3: The long-term recommendation is to use source generators in such scenarios but the support to make it easier for developers to use source generators are not planned in the near term.
Risks
This API might cause more code to be preserved than intended since it applies to all derived types of the targetType. This can be mitigated through requiring an attribute to be also applied to the parent type.
Activity
LakshanF commentedon Mar 10, 2021
cc @vitek-karas, @MichalStrehovsky
LakshanF commentedon Mar 22, 2021
With #49465 approved and mostly unblocks the basic scenarios related to object.GetType(). Once the trimmer support is added (currently in progress) to recognize the attribute usage on classes, this API request is not a high-priority for the trimmer team in 6.0.
eerhardt commentedon May 10, 2021
Can we close this issue now that #49465 has been fixed?
MichalStrehovsky commentedon May 10, 2021
I'm not aware of a scenario for this right now - we addressed what we had with #49465. This one is kind of more powerful, but if there's no scenario to validate it on, we should close. (Reopen if anyone has one.)