Replies: 2 comments 2 replies
-
You can implement [return: NotNullIfNotNull(nameof(value))]
public static unsafe TTo? CastSame<TFrom, TTo>(TFrom? value)
where TFrom : allows ref struct
where TTo : allows ref struct
{
if (typeof(TTo) != typeof(TFrom))
{
throw new InvalidCastException();
}
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
return *(TTo*)&value;
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
For value types (including ref structs) you can use Once you've done the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Does .NET have build-in helper method with semantic like this?
So you can use it in generic context like this:
Beta Was this translation helpful? Give feedback.
All reactions