Skip to content
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 @@ -4,6 +4,7 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WindowsRuntime.InteropServices.Marshalling;

Expand All @@ -14,6 +15,11 @@ namespace WindowsRuntime.InteropServices;
/// <summary>
/// A marshaller with some utility methods that directly wrap <see cref="ComWrappers"/>.
/// </summary>
/// <remarks>
/// No method in this class performs input validation. If any parameter is <see langword="null"/>,
/// the code will throw <see cref="NullReferenceException"/>. It is the caller's responsibility
/// to validate inputs before calling any method in this class.
/// </remarks>
[Obsolete(WindowsRuntimeConstants.PrivateImplementationDetailObsoleteMessage,
DiagnosticId = WindowsRuntimeConstants.PrivateImplementationDetailObsoleteDiagnosticId,
UrlFormat = WindowsRuntimeConstants.CsWinRTDiagnosticsUrlFormat)]
Expand Down Expand Up @@ -206,4 +212,23 @@ public static bool TryUnwrapObjectReference(
return false;
}
}

/// <summary>
/// Unwraps the <see cref="WindowsRuntimeObjectReference"/> from the specified <see cref="WindowsRuntimeObject"/>
/// instance and returns it directly.
/// </summary>
/// <param name="value">The <see cref="WindowsRuntimeObject"/> instance to unwrap.</param>
/// <returns>The <see cref="WindowsRuntimeObjectReference"/> wrapping the native object from <paramref name="value"/>.</returns>
/// <remarks>
/// <para>
/// This method does not validate whether <paramref name="value"/> can actually be unwrapped (i.e. whether
/// <see cref="WindowsRuntimeObject.HasUnwrappableNativeObjectReference"/> is <see langword="true"/>). It is
/// the caller's responsibility to ensure that the object is in a valid state for unwrapping.
/// </para>
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WindowsRuntimeObjectReference UnwrapObjectReferenceUnsafe(WindowsRuntimeObject value)
{
return value.NativeObjectReference;
}
}
7 changes: 5 additions & 2 deletions src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9350,10 +9350,13 @@ return (%?)%.ConvertToManaged<%ComWrappersCallback>(value);
{
if (sealed)
{
// For projected sealed runtime classes, the RCW type is always unwrappable (as the
// type can never be subclassed), so we can use UnwrapObjectReferenceUnsafe directly
// instead of going through the TryUnwrapObjectReference check.
w.write(R"(
if (WindowsRuntimeComWrappersMarshal.TryUnwrapObjectReference(value, out WindowsRuntimeObjectReference? objectReference))
if (value is not null)
{
return objectReference.AsValue();
return WindowsRuntimeComWrappersMarshal.UnwrapObjectReferenceUnsafe(value).AsValue();
}
)");
}
Expand Down