-
Notifications
You must be signed in to change notification settings - Fork 107
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
First iteration of making CsWinRT compatible with IL trimming #1224
Merged
Conversation
This file contains 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
…ad of type lookup for helper types.
…ype name and attempt to address issue where Xaml metadata providers asks for trimmed types.
Closed
jkoritzinsky
approved these changes
Jul 14, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth creating some internal constants in WinRT.Runtime for some common bitmasks of the DynamicallyAccessedMemberTypes enum to reduce duplication.
Other than that, LGTM!
huoyaoyuan
reviewed
Jul 16, 2022
j0shuams
approved these changes
Jul 18, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR addresses a good portion of the IL trimming warnings and makes CsWinRT IL trimming compatible. Some of the major areas addressed are the following:
CsWinRT today uses naming conventions and Type.GetType to find the helper type associated with a given type. This change adds a new attribute,
WindowsRuntimeHelperType
, which is projected onto all types other than enums. This attribute has a property which provides access to the helper type. Given a type, CsWinRT looks for this attribute and uses it to get the helper type without needing to worry about it getting trimmed or using GetType. The GetType calls are still kept, but warnings are suppressed as we do need a fallback for older projections that haven't been updated yet.During FromABI scenarios, CsWinRT creates an RCW instance by using reflection to call an internal constructor on the RCW type being created. The code has been attributed to specify we dynamically access the non-public constructor.
There can be scenarios where the app receives an object for which it only interacts with it using one of the base classes. This means the actual type of the object (derived type) can be trimmed. Added support to handle this by making projections register a dictionary of all their Child -> Base class mappings using
RegisterProjectionTypeBaseTypeMapping
which is consulted in the case that a type to construct cannot be found. If it is found the child class doesn't exist, but the base class does, then it is used to construct the RCW.Helper types have been attributed to specify everything we access from them such as during marshaling.
CsWinRT has a custom Type projection which is used by XAML to look up types. XAML can end up looking up types that have been trimmed as there are no references to it in C# code but is still used in native code. For these scenarios, we return a fake type instance to represent that type as this is what .NET native used to do for these scenarios.
As part of validating these changes, added a set of functional tests which exercise trimming and validate core functionality. Working on collecting stats for size improvements but the functional tests do demonstrate types are getting trimmed and the DLLs are much smaller in the basic scenarios.
There is still a bunch of MakeGenericType calls that need to be suppressed but that will be addressed in a future PR as I do not want to suppress the entire function they are used as part of for trim warnings.
#373