Last active
April 27, 2022 16:16
Declare an object as an instance of System.Math.
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; | |
using System.Linq; | |
using System.Reflection; | |
class Program | |
{ | |
class MyClass | |
{ | |
} | |
static void Main(string[] args) | |
{ | |
var mscorlib = AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetType("System.RuntimeType", false, true) != null); | |
var runtimeType = mscorlib.GetType("System.RuntimeType"); | |
var handle = runtimeType.GetField("m_handle", BindingFlags.Instance | BindingFlags.NonPublic); | |
var mathTable = handle.GetValue(typeof(Math)); | |
handle.SetValue(typeof(MyClass), mathTable); | |
var o = new MyClass(); | |
Console.WriteLine(o.GetType()); // System.Math | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment