Skip to content

Instantly share code, notes, and snippets.

@AliveDevil
Last active July 13, 2023 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliveDevil/a16e2e24f4a77b6bd2dc362e0a80e7db to your computer and use it in GitHub Desktop.
Save AliveDevil/a16e2e24f4a77b6bd2dc362e0a80e7db to your computer and use it in GitHub Desktop.
Cursed.
using System.Runtime.CompilerServices;
Console.WriteLine("Hello, World!");
ConcreteWindow w = new();
Window<int> a = Unsafe.As<Window<int>>(w);
a.Show();
;
public interface IViewFor<T> { }
public class WindowBase
{
public virtual void Show()
{
}
}
public sealed class Window<T> : WindowBase, IViewFor<T>
{
}
public class ConcreteWindow : WindowBase, IViewFor<int>
{
public override void Show()
{
base.Show();
}
}
Window<int> b = ReinterpretCast<ConcreteWindow, Window<int>>(w);
static unsafe TDest ReinterpretCast<TSource, TDest>(TSource source)
{
var sourceRef = __makeref(source);
var dest = default(TDest);
var destRef = __makeref(dest);
*(nint*)&destRef = *(nint*)&sourceRef;
return __refvalue(destRef, TDest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment