ExtraLanscapingToolsCommon.Redirection.RedirectionHelper.RedirectCalls C# (CSharp) Method

RedirectCalls() public static method

Redirects all calls from method 'from' to method 'to'.
public static RedirectCalls ( MethodInfo from, MethodInfo to ) : RedirectCallsState
from System.Reflection.MethodInfo
to System.Reflection.MethodInfo
return RedirectCallsState
        public static RedirectCallsState RedirectCalls(MethodInfo from, MethodInfo to)
        {
            // GetFunctionPointer enforces compilation of the method.
            var fptr1 = from.MethodHandle.GetFunctionPointer();
            var fptr2 = to.MethodHandle.GetFunctionPointer();
            return PatchJumpTo(fptr1, fptr2);
        }

Usage Example

Beispiel #1
0
        private static Tuple <MethodInfo, RedirectCallsState> RedirectMethod(Type targetType, MethodInfo detour, bool reverse)
        {
            var parameters = detour.GetParameters();

            Type[] types;
            if (parameters.Length > 0 && (
                    (!targetType.IsValueType && parameters[0].ParameterType == targetType) ||
                    (targetType.IsValueType && parameters[0].ParameterType == targetType.MakeByRefType())))
            {
                types = parameters.Skip(1).Select(p => p.ParameterType).ToArray();
            }
            else
            {
                types = parameters.Select(p => p.ParameterType).ToArray();
            }
            var originalMethod = targetType.GetMethod(detour.Name,
                                                      BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, types,
                                                      null);
            var redirectCallsState =
                reverse ? RedirectionHelper.RedirectCalls(detour, originalMethod) : RedirectionHelper.RedirectCalls(originalMethod, detour);

            return(Tuple.New(reverse ? detour : originalMethod, redirectCallsState));
        }