Пример #1
0
        /// <summary>
        /// Gets the class that the method will operate in the context of.
        /// This is NOT necessarily the class that the method exists in.
        /// For extension methods the "this" parameter (first) will be returned
        /// regardless of whether it is a detour or not, pure static methods
        /// will return null (again, regardless of being a detour) as they
        /// don't operate in the context of class.  Instance methods will
        /// return the defining class for non-detours and the class being
        /// injected into for detours.
        /// </summary>
        /// <returns>The method target class</returns>
        /// <param name="info">MethodInfo of the method to check</param>
        /// <param name="attribute">DetourMember attribute</param>
        private static Type GetMethodTargetClass(MethodBase info, DetourMemberBase attribute = null)
        {
            var methodType = GetMethodType(info);

            if (methodType == MethodType.Static)
            {               // Pure static methods don't have a target class
                return(null);
            }
            if (methodType == MethodType.Extension)
            {               // Regardless of whether this is the detour method or the method to be detoured, for extension methods we take the target class from the first parameter
                return(info.GetParameters()[0].ParameterType);
            }

            if (attribute == null)
            {
                info.TryGetAttribute(out attribute);
            }
            if (attribute != null)
            {
                if (attribute.targetClass != DetourMemberBase.DefaultTargetClass)
                {
                    return(attribute.targetClass);
                }
                return(info.DeclaringType.BaseType);
            }

            return(info.DeclaringType);
        }
Пример #2
0
        /// <summary>
        /// Gets the class that a detour will be injected into.
        /// </summary>
        /// <returns>The detour target class</returns>
        /// <param name="info">MemberInfo of the member to check</param>
        /// <param name="attribute">DetourMember attribute</param>
        private static Type GetDetourTargetClass(MemberInfo info, DetourMemberBase attribute)
        {
            if (attribute != null)
            {
                if (attribute.targetClass != DetourMemberBase.DefaultTargetClass)
                {
                    return(attribute.targetClass);
                }
                var methodInfo = info as MethodInfo;
                if (
                    (info.DeclaringType.BaseType == typeof(Object)) &&
                    (methodInfo != null) &&
                    (GetMethodType(methodInfo) == MethodType.Extension)
                    )
                {
                    return(methodInfo.GetParameters()[0].ParameterType);
                }
                return(info.DeclaringType.BaseType);
            }

            return(info.DeclaringType);
        }