Skip to content

Bug: Overridable properties are not used in CCWs #2145

@Ryken100

Description

@Ryken100

Description

Overridable properties in WinRT types are not used in CCWs; overriding a property results in the default implementation from the original method table being used, instead of the user defined implementation of the property.

The issue can be seen clearly in the generated class for LayoutState. The explicit ILayoutContextOverrides.LayoutStateCore implementation does not call the virtual LayoutStateCore property, instead calling into the original method table for the interface. This results in the overridden LayoutStateCore implementation not being used in the CCW.

    object ILayoutContextOverrides.LayoutStateCore
    {
        get
        {
            return ILayoutContextOverridesMethods.get_LayoutStateCore(_objRef_global__Microsoft_UI_Xaml_Controls_ILayoutContextOverrides);
        }
        set
        {
            ILayoutContextOverridesMethods.set_LayoutStateCore(_objRef_global__Microsoft_UI_Xaml_Controls_ILayoutContextOverrides, value);
        }
    }

    protected virtual object LayoutStateCore
    {
        get
        {
            return ILayoutContextOverridesMethods.get_LayoutStateCore(_objRef_global__Microsoft_UI_Xaml_Controls_ILayoutContextOverrides);
        }
        set
        {
            ILayoutContextOverridesMethods.set_LayoutStateCore(_objRef_global__Microsoft_UI_Xaml_Controls_ILayoutContextOverrides, value);
        }
    }

For example, if a user were to create a type that derives from NonVirtualizngLayoutContext and overrode LayoutStateCore to return an object, only the original implementation (which returns E_NOTIMPL) would be visible to WinRT objects, not the user-defined implementation.

Steps To Reproduce

  1. Create a new WinUI app in Visual Studio.
  2. Create a type that derives from NonVirtualizingLayoutContext.
  3. Override LayoutStateCore to return a value (ex. it can be an auto property with new object() as the default value).
  4. Create an instance of the derived type and try to read its LayoutState property.
  5. Observe that NotImplementedException is thrown instead of the value from step 3 being returned.

Expected Behavior

Overridable properties should be used in and callable via CCWs.

Version Info

CsWinRT: 2.3.0-prerelease.251115.2

OverridablePropertyRepro.zip

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions