Last active
December 20, 2016 21:14
-
-
Save mwolicki/fc8d94be89a88b2aa391921c7ce39d34 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
namespace Example | |
{ | |
class MainClass | |
{ | |
static void Main() | |
{ | |
Display(0); | |
Display(new A(6)); | |
Display2(new A(6)); | |
} | |
static void Display(object a) | |
{ | |
Console.WriteLine("----------------"); | |
Console.WriteLine(a.GetType().FullName); | |
reinterpret_cast<string>(a); | |
Console.WriteLine(a.GetType().FullName); | |
Console.WriteLine((string)a); | |
} | |
static void Display2(A a) | |
{ | |
Console.WriteLine("+++++++++++++++++"); | |
Console.WriteLine(a.GetType().FullName); | |
reinterpret_cast<B>(a); | |
Console.WriteLine(a.GetType().FullName); | |
var b = (B)(object)a; | |
Console.WriteLine($"{b.M}{b.C}{b.W}{a.X()}"); | |
} | |
//TODO: consider GC... | |
unsafe static void reinterpret_cast<T>(object obj) | |
{ | |
var tr = __makeref(obj); | |
var typeHandle = **(IntPtr***)&tr; | |
*typeHandle = typeof(T).TypeHandle.Value; | |
} | |
} | |
class A | |
{ | |
public int I; | |
public readonly char M = 'T'; | |
public readonly char a = 'e'; | |
public readonly char r = 's'; | |
public readonly char c = 't'; | |
public readonly char i = ':'; | |
public readonly char n = ')'; | |
public A(int i) | |
{ | |
I = i; | |
} | |
public /*virtual*/ string X() => "(some text)"; | |
} | |
class B | |
{ | |
public int I; | |
public readonly char M = 'M'; | |
public readonly char C = 'C'; | |
public readonly char W = 'W'; | |
public B(int i) | |
{ | |
I = i; | |
} | |
public /*virtual*/ string X() => "(XXXX XXXX)"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment