/// <summary>
        /// Disables the override of a method(WARNING: The method needs to have been overridden atleast once!)
        /// </summary>
        /// <param name="method">The original method that was Overrideed</param>
        /// <returns>If the Override was disabled successfully</returns>
        public static bool DisableOverride(MethodInfo method)
        {
            // Set the variables
            OverrideWrapper wrapper = OverrideManager.Overrides.First(a => a.Value.Original == method).Value;

            // Do the checks
            return(wrapper != null && wrapper.Revert());
        }
        /// <summary>
        /// Calls the original method that was Overrideed
        /// </summary>
        /// <param name="method">The original method</param>
        /// <param name="instance">The instance for the method(null if static)</param>
        /// <param name="args">The arguments for the method</param>
        /// <returns>The value that the original function returns</returns>
        public static object CallOriginalFunc(MethodInfo method, object instance = null, params object[] args)
        {
            // Do the checks
            if (OverrideManager.Overrides.All(o => o.Value.Original != method))
            {
                throw new Exception("The Override specified was not found!");
            }

            // Set the variables
            OverrideWrapper wrapper = OverrideManager.Overrides.First(a => a.Value.Original == method).Value;

            return(wrapper.CallOriginal(args, instance));
        }
Пример #3
0
        /// <summary>
        /// Loads override information for method
        /// </summary>
        /// <param name="method">Method to override another</param>
        public static void LoadOverride(MethodInfo method)
        {
            // Get attribute related variables
            OverrideAttribute attribute =
                (OverrideAttribute)Attribute.GetCustomAttribute(method, typeof(OverrideAttribute));

            // Check if method has been overrided before
            if (Overrides.Count(a => a.Key.Method == attribute.Method) > 0)
            {
                return;
            }

            // Create wrapper for override
            OverrideWrapper wrapper = new OverrideWrapper(attribute.Method, method, attribute);

            // Override
            wrapper.Override();

            // Add override to the list
            Overrides.Add(attribute, wrapper);
        }
        /// <summary>
        /// Calls the original method that was Overrideed
        /// </summary>
        /// <param name="instance">The instance for the method(null if static)</param>
        /// <param name="args">The arguments for the method</param>
        /// <returns>The value tahat the original function returns</returns>
        public static object CallOriginal(object instance = null, params object[] args)
        {
            StackTrace trace = new StackTrace(false);

            if (trace.FrameCount < 1)
            {
                throw new Exception("Invalid trace back to the original method! Please provide the methodinfo instead!");
            }

            MethodBase modded   = trace.GetFrame(1).GetMethod();
            MethodInfo original = null;

            if (!Attribute.IsDefined(modded, typeof(OverrideAttribute)))
            {
                modded = trace.GetFrame(2).GetMethod();
            }
            OverrideAttribute att = (OverrideAttribute)Attribute.GetCustomAttribute(modded, typeof(OverrideAttribute));

            if (att == null)
            {
                throw new Exception("This method can only be called from an overwritten method!");
            }
            if (!att.MethodFound)
            {
                throw new Exception("The original method was never found!");
            }
            original = att.Method;

            if (OverrideManager.Overrides.All(o => o.Value.Original != original))
            {
                throw new Exception("The Override specified was not found!");
            }

            OverrideWrapper wrapper = OverrideManager.Overrides.First(a => a.Value.Original == original).Value;

            return(wrapper.CallOriginal(args, instance));
        }