Skip to content

Add feature switches for Type marshalling and IDIC support#2223

Draft
Sergio0694 wants to merge 10 commits intouser/kythant/TypeHandlingfrom
user/sergiopedri/type-marshalling-tweaks
Draft

Add feature switches for Type marshalling and IDIC support#2223
Sergio0694 wants to merge 10 commits intouser/kythant/TypeHandlingfrom
user/sergiopedri/type-marshalling-tweaks

Conversation

@Sergio0694
Copy link
Member

@Sergio0694 Sergio0694 commented Feb 4, 2026

Title. It also enables caching marshalling results for Type.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds two feature switches to support trimming optimization in CsWinRT: one for Type marshalling and one for IDynamicInterfaceCastable (IDIC) support. Both features default to enabled but can be disabled to reduce application size when not needed.

Changes:

  • Added EnableXamlTypeMarshalling and EnableIDynamicInterfaceCastableSupport feature switches with corresponding MSBuild properties
  • Refactored Type marshalling with improved caching using ConcurrentDictionary and alternate lookups for better performance
  • Changed internal type map tables from ConditionalWeakTable to ConcurrentDictionary for trimming compatibility

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/WinRT.Runtime2/Properties/WindowsRuntimeFeatureSwitches.cs Added two new feature switch properties with configuration names
src/WinRT.Runtime2/WindowsRuntimeObject.cs Added feature switch checks in IDIC methods, new exception helper class, and fixed comment reference
src/WinRT.Runtime2/InteropServices/WindowsRuntimeComWrappers.cs Added feature switch guard for Type marshalling
src/WinRT.Runtime2/ABI/System/Type.cs Major refactoring: new caching infrastructure, helper types for zero-allocation lookups, and feature switch integration
src/WinRT.Runtime2/InteropServices/TypeMapInfo/WindowsRuntimeMetadataInfo.cs Changed from ConditionalWeakTable to ConcurrentDictionary
src/WinRT.Runtime2/InteropServices/TypeMapInfo/WindowsRuntimeMarshallingInfo.cs Changed from ConditionalWeakTable to ConcurrentDictionary
src/WinRT.Runtime2/InteropServices/TypeMapInfo/DynamicInterfaceCastableImplementationInfo.cs Changed from ConditionalWeakTable to ConcurrentDictionary, added System.Collections.Concurrent import
nuget/Microsoft.Windows.CsWinRT.targets Added MSBuild properties and runtime configuration for new feature switches, removed obsolete switches

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sergio0694 and others added 9 commits February 4, 2026 20:57
Replace the per-Type backing tables from ConditionalWeakTable<Type, T?> to ConcurrentDictionary<Type, T?> in DynamicInterfaceCastableImplementationInfo, WindowsRuntimeMarshallingInfo, and WindowsRuntimeMetadataInfo. Also add a using System.Collections.Concurrent directive where needed. Note: this changes lifetime semantics (ConcurrentDictionary holds strong references), which may affect object garbage-collection behavior compared to ConditionalWeakTable.
Introduce zero-allocation marshalling helpers and caches for System.Type handling. Adds ManagedTypeReference and TransientTypeReference types, a ManagedTypeReferenceEqualityComparer, and a TypeNameCache (two ConcurrentDictionaries optimized for UI-thread access) to enable alternate-key lookups and avoid string allocations. Split out UncachedTypeMarshaller with FromTransientTypeReference/ToManagedTypeReference to centralize non-cached logic, and update TypeMarshaller to use the cache paths and alternate lookups. Adjust Convert/Box/Unbox/Dispose flows accordingly. Remove the NoMetadataTypeInfo cache and make its constructor public so trimmed/metadata-missing types are created on demand.
Introduce a new feature switch in WindowsRuntimeFeatureSwitches to control marshalling of Type instances for XAML projections. Adds the configuration constant CSWINRT_ENABLE_XAML_TYPE_MARSHALLING and the EnableXamlTypeMarshalling property (FeatureSwitchDefinition) with a default of true, along with XML documentation.
Make System.Type marshalling conditional on WindowsRuntimeFeatureSwitches.EnableXamlTypeMarshalling. Added runtime checks in TypeMarshaller ConvertFrom/ConvertTo to throw a NotSupportedException when XAML type marshalling is disabled, and added TypeExceptions.ThrowNotSupportedExceptionForMarshallingDisabled to provide a clear error message. Updated WindowsRuntimeComWrappers to only return Type marshalling info when the feature switch is enabled to avoid rooting extra metadata/type-mapping code and reduce binary size.
Delete default property values and their RuntimeHostConfigurationOption entries for several CsWinRT feature switches in nuget/Microsoft.Windows.CsWinRT.targets. Removed properties: CsWinRTEnableDynamicObjectsSupport, CsWinRTUseExceptionResourceKeys, CsWinRTEnableDefaultCustomTypeMappings, CsWinRTEnableICustomPropertyProviderSupport, and CsWinRTEnableIReferenceSupport. This cleans up obsolete/unused runtime feature switches while leaving other configuration options (e.g. IDynamicInterfaceCastable, ManifestFreeActivation) intact.
Introduce an MSBuild property CsWinRTEnableXamlTypeMarshalling (default true) and map it to the runtime host configuration option CSWINRT_ENABLE_XAML_TYPE_MARSHALLING in nuget/Microsoft.Windows.CsWinRT.targets. This exposes a build-time switch to enable/disable XAML type marshalling behavior alongside the other CsWinRT runtime switches.
Introduce a configurable feature switch to control IDynamicInterfaceCastable support so the runtime can be trimmed when this capability is not needed.

- nuget: rename the RuntimeHostConfigurationOption to CSWINRT_ENABLE_IDYNAMICINTERFACECASTABLE_SUPPORT and wire it to the CsWinRTEnableIDynamicInterfaceCastableSupport property.
- src/WinRT.Runtime2: add EnableIDynamicInterfaceCastableSupport feature switch and default it to true.
- src/WinRT.Runtime2/WindowsRuntimeObject: gate IDynamicInterfaceCastable APIs with the feature switch (throw NotSupportedException or return false when disabled) to allow trimming and to ensure unreachable paths can be removed.

Also include a small doc comment polish for an IEnumerable reference.
Replace the inline ThrowNotSupportedException local with a file-scoped WindowsRuntimeObjectExceptions.ThrowNotSupportedException and update IDynamicInterfaceCastable methods to perform an early feature-switch check. This centralizes the NotSupportedException stub (DoesNotReturn/StackTraceHidden) to improve trimming and reuse, and ensures IsInterfaceImplemented throws the same descriptive NotSupportedException when throwIfNotImplemented is true.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/type-marshalling-tweaks branch from f3d9477 to d1fe0c7 Compare February 5, 2026 04:58
Reorder the Type marshalling check to first verify WindowsRuntimeFeatureSwitches.EnableXamlTypeMarshalling so the 'Type' cast can be trimmed and avoid rooting the metadata type map (reducing binary size). Also simplify the GetInfo call to use the local method (GetInfo(typeof(Type))). Comments updated to reflect the new ordering.
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.

1 participant