Table of Contents

Method TryGet

Namespace
Ubiquity.NET.InteropHelpers
Assembly
Ubiquity.NET.InteropHelpers.dll

TryGet<T>(void*, out T)

Try pattern to convert a GCHandle as a raw native void pointer

public static bool TryGet<T>(void* ctx, out T value)

Parameters

ctx void*

Native pointer for the context

value T

Value of the context or default if return is false

Returns

bool

true if the ctx is valid and converted to value

Type Parameters

T

Type of object the context should hold

Remarks

This assumes that ctx is a GCHandle that was allocated and provided to native code via a call to ToIntPtr(GCHandle). This will follow the try pattern to resolve ctx back to the original instance the handle is referencing. This allows managed code callbacks to use managed objects as an opaque "context" value for native APIs.

important

In order to satisfy nullability code analysis, call sites must declare T explicitly. Otherwise, it is deduced as the type used for value, which will cause analysis to complain if it isn't a nullable type. Thus, without explicit declaration of the type without nullability it is assumed nullable and the MaybeNullWhenAttribute is effectively moot. So call sites should always specify the generic type parameter T explicitly.