Skip to content
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
merged 33 commits into from
Jul 19, 2022

Conversation

manodasanW
Copy link
Member

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:

  • Helper type lookup
    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.
  • RCW construction
    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.
  • Composable types
    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.
  • Marshaling
    Helper types have been attributed to specify everything we access from them such as during marshaling.
  • Xaml Type lookup
    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

Copy link
Member

@jkoritzinsky jkoritzinsky left a 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!

src/WinRT.Runtime/Projections/Type.cs Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants