microsoft / CsWinRT

C# language projection for the Windows Runtime
MIT License
554 stars 105 forks source link

InvaildCastException occurs when the argument is passed in as a collection expression #1729

Open Gaoyifei1011 opened 2 months ago

Gaoyifei1011 commented 2 months ago

Describe the bug

InvaildCastException occurs when the argument is passed in as a collection expression 传入参数为集合表达式时会发生 InvaildCastException

To Reproduce

Compositor. CreateEffectFactory() have multiple overloaded method, one of the second parameter for IEnumerable, at this time if in the second parameter to set expression directly, causing InvaildCastException Compositor.CreateEffectFactory() 方法有多个重载,其中一个第二个参数为 IEnumerable,这时如果在第二个参数中直接传入集合表达式,引发 InvaildCastException {CC80183B-4C3B-431D-93F0-ECBFCE4FEAFB} caf934614c1344212b350007c479ec7f f13e29d919a261020e21de40b27c3622 9cb94d616e2a7dba14cb33d7ce92c3e4

Expected behavior

Provide warning or support collection expression in parameter

Version Info

CsWinRT 2.1.1 Windows SDK 10.0.26100.0 .NET SDK 9.0.0-preview7

Additional context

Found in the process of debugging, set the type of the expression is <>z_ReadOnlySingleElementList 调试的过程中,发现集合表达式生成的类型是<>z_ReadOnlySingleElementList 646565e7042e4e16ff4a2f15458a9c6e

hez2010 commented 2 months ago

@manodasanW I think we should not use source generator at the first place, instead we should generate an assembly just like what WPF does, so that things like this can get covered.

manodasanW commented 2 months ago

Sergio did bring up a related idea with separate assembly for the generic instantiations and vtable lookup table that is being considered. But I don't think that would solve this problem where these are internal .NET types that CsWinRT won't directly be aware of unless you were thinking of something there?

The plan to address that is to make use of the statically known interface on the projection and provide a vtable for that when the one for the class isn't available. This is tracked by #1661. The tricky part will be minimizing size impact.