Last active
July 13, 2023 10:16
-
-
Save AliveDevil/a16e2e24f4a77b6bd2dc362e0a80e7db to your computer and use it in GitHub Desktop.
Cursed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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