Skip to content

Fix CA2256 in COM generator when using 'ManagedObjectWrapper' #114642

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

Merged
merged 2 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
using StringWriter source = new();
source.WriteLine("// <auto-generated />");
source.WriteLine("#pragma warning disable CS0612, CS0618"); // Suppress warnings about [Obsolete] member usage in generated code.

// If the user has specified 'ManagedObjectWrapper', it means that the COM interface will never be used to marshal a native
// object as an RCW (eg. the IDIC vtable will also not be generated, nor any additional supporting code). To reduce binary
// size, we're not emitting the interface methods on the implementation interface that has '[DynamicInterfaceCastableImplementation]'
// on it. However, doing so will cause the CA2256 warning to be produced. We can't remove the attribute, as that would cause
// the wrong exception to be thrown when trying an IDIC cast with this interface (not 'InvalidCastException'). Because this is
// a niche scenario, and we don't want to regress perf or size, we can just disable the warning instead.
if (interfaceContext.Options is ComInterfaceOptions.ManagedObjectWrapper)
{
source.WriteLine("#pragma warning disable CA2256");
}

interfaceInfo.WriteTo(source);
// Two newlines looks cleaner than one
source.WriteLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public enum ComInterfaceOptions
/// <summary>
/// Generate a wrapper for managed objects to enable exposing them through the COM interface.
/// </summary>
/// <remarks>
/// <para>
/// When this flag is the only one specified on a given COM interface, no implementation methods will be generated
/// to support <see cref="System.Runtime.InteropServices.IDynamicInterfaceCastable"/> casts. In such a scenario,
/// attempting to cast to that interface type will still succeed, but any calls to interface methods will fail.
/// </para>
/// <para>
/// Using only this flag is purely a binary size optimization. If calling methods via this interface on native
/// object is required, the <see cref="ComObjectWrapper"/> flag should also be used instead.
/// </para>
/// </remarks>
ManagedObjectWrapper = 0x1,
/// <summary>
/// Generate a wrapper for COM objects to enable exposing them through the managed interface.
Expand Down
Loading