private static IntPtr GetDynamicMethodAddress(MethodBase method) { unsafe { var handle = GetDynamicMethodRuntimeHandle(method); var ptr = (byte *)handle.Value.ToPointer(); if (IsNet20Sp2OrGreater()) { RuntimeHelpers.PrepareMethod(handle); return(handle.GetFunctionPointer()); } else { if (IntPtr.Size == 8) { var address = (ulong *)ptr; address = (ulong *)*(address + 5); return(new IntPtr(address + 12)); } else { var address = (uint *)ptr; address = (uint *)*(address + 5); return(new IntPtr(address + 12)); } } } }
/// <summary> /// Gets the address of the method stub /// </summary> /// <param name="method">The method handle.</param> /// <returns></returns> public static IntPtr GetMethodAddress(MethodBase method) { if ((method is DynamicMethod)) { return(GetDynamicMethodAddress(method)); } // Prepare the method so it gets jited RuntimeHelpers.PrepareMethod(method.MethodHandle); return(method.MethodHandle.GetFunctionPointer()); }
private static IntPtr GetDynamicMethodAddress(MethodBase method) { unsafe { RuntimeMethodHandle handle = GetDynamicMethodRuntimeHandle(method); byte *ptr = (byte *)handle.Value.ToPointer(); if (IsNet20Sp2OrGreater()) { RuntimeHelpers.PrepareMethod(handle); return(handle.GetFunctionPointer()); //if (IntPtr.Size == 8) //{ // ulong* address = (ulong*)ptr; // address = (ulong*)*(address + 5); // return new IntPtr(address + 12); //} //else //{ // uint* address = (uint*)ptr; // address = (uint*)*(address + 5); // return new IntPtr(address + 12); //} } else { if (IntPtr.Size == 8) { ulong *address = (ulong *)ptr; address += 6; return(new IntPtr(address)); } else { uint *address = (uint *)ptr; address += 6; return(new IntPtr(address)); } } } }
/// <summary> /// Gets the address of the method stub /// </summary> /// <param name="method">The method handle.</param> /// <returns></returns> public static IntPtr GetMethodAddress(MethodBase method) { if ((method is DynamicMethod)) { return(GetDynamicMethodAddress(method)); } // Prepare the method so it gets jited RuntimeHelpers.PrepareMethod(method.MethodHandle); // If 3.5 sp1 or greater than we have a different layout in memory. if (IsNet20Sp2OrGreater()) { return(GetMethodAddress20SP2(method)); } unsafe { // Skip these const int skip = 10; // Read the method index. var location = (UInt64 *)(method.MethodHandle.Value.ToPointer()); var index = (int)(((*location) >> 32) & 0xFF); if (IntPtr.Size == 8) { // Get the method table var classStart = (ulong *)method.DeclaringType.TypeHandle.Value.ToPointer(); var address = classStart + index + skip; return(new IntPtr(address)); } else { // Get the method table uint *classStart = (uint *)method.DeclaringType.TypeHandle.Value.ToPointer(); uint *address = classStart + index + skip; return(new IntPtr(address)); } } }