Emit IComIID on downlevel TFMs (net472 / netstandard2.0)#1705
Open
JeremyKuhne wants to merge 1 commit into
Open
Emit IComIID on downlevel TFMs (net472 / netstandard2.0)#1705JeremyKuhne wants to merge 1 commit into
IComIID on downlevel TFMs (net472 / netstandard2.0)#1705JeremyKuhne wants to merge 1 commit into
Conversation
Fixes microsoft#1704. On TFMs that do not support static abstract interface members, CsWin32 previously skipped emitting the IComIID interface and did not attach it to generated COM struct wrappers. Consumers multi-targeting net10.0 + net472 had to hand-author per-struct partial declarations to polyfill the missing pieces. Now the generator emits an instance-form polyfill that matches the dotnet/winforms IDataObject.cs / dotnet/msbuild IComIIDPolyfills.cs shape exactly: internal interface IComIID { internal ref readonly Guid Guid { get; } } internal partial struct Foo : IComIID { readonly ref readonly Guid IComIID.Guid { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => ref Unsafe.AsRef(in IID_Guid); } } On net7.0+ the optimized static-abstract form (with the permanent-address Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(...)) pattern) is unchanged. Adds regression tests covering net472, netstandard2.0, net8.0, net9.0, and net10.0, including a multi-targeting consumer that uses IComIID as a generic constraint on every TFM.
This file contains hidden or 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
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.
Fixes #1704.
Summary
On target frameworks that do not support static abstract interface members (
net472,netstandard2.0), CsWin32 previously skipped emitting theIComIIDinterface entirely and did not attach it to generated COM struct wrappers. Consumers multi-targeting (e.g.net10.0;net472) had to hand-author two polyfill files per project — an instance-formIComIIDinterface plus onepartial struct Foo : IComIID { Guid IComIID.Guid => IID_Guid; }per generated COM type used throughComScope<T>— and add a new partial every time a COM type was added toNativeMethods.txt. See dotnet/msbuild'sIComIIDPolyfills.csand dotnet/winforms'sIDataObject.csfor the workarounds in production today.Fix
The generator now emits
IComIIDon every TFM. On TFMs that support static abstract interface members the existing optimized form is unchanged. On downlevel TFMs the generator emits an instance-form polyfill matching the shape used bydotnet/winformsanddotnet/msbuildbyte-for-byte:The uniform
ref readonly Guid Guid { get; }signature across both forms means a single consumer signature works on every TFM — only thestatic abstractmodifiers differ. Adopters can delete their hand-authored polyfills entirely on upgrade.Tests
COMInterfaceIIDInterfaceOnDownlevelTFMstest asserts the interface declaration, the explicit-interface implementation, and theref Unsafe.AsRef(in IID_Guid)body shape onnet472andnetstandard2.0.IComIID_MultiTargeting_Issue1704theory mirrors the exactNativeMethods.txtfrom issueIComIIDis not emitted onnet472/netstandard2.0#1704 againstnet472,netstandard2.0,net8.0,net9.0, andnet10.0. It generatesGetRunningObjectTable,IRunningObjectTable,IMoniker,CoCreateInstance, asserts every COM struct listsIComIIDin its base list, and compiles a consumer snippet usingwhere T : unmanaged, IComIIDas a generic constraint on every TFM.Manual repro verification
Built the exact repro project from #1704 (
TargetFrameworks=net10.0;net472):0.3.275→error CS0246: 'IComIID' could not be foundon net472 ✅ (reproduced)dotnet runon net10.0 prints00000010-0000-0000-c000-000000000046✅ (fixed)Inspected the per-TFM generated output:
net10.0:internal static abstract ref readonly Guid Guid { get; }(unchanged optimal form)net472:internal ref readonly Guid Guid { get; }+readonly ref readonly Guid IComIID.Guid => ref Unsafe.AsRef(in IID_Guid);(new)