コード例 #1
0
        /// <summary>
        /// Decides if <paramref name="method"/> is an implementation to some of the methods declared in <paramref name="@interface"/>.
        /// </summary>
        /// <param name="method">The target method.</param>
        /// <param name="interface">The interface supposedly containing the target method's declaration.</param>
        /// <returns>Returns true, if the target method implements one of interface's methods.</returns>
        public static bool IsImplementationOf(this MethodReference method, TypeDefinition @interface)
        {
            if (@interface == null)
            {
                throw new ArgumentNullException("@interface can not be null.");
            }
            if ([email protected])
            {
                throw new ArgumentOutOfRangeException("The @interface argument is not an interface definition.");
            }

            if (method.DeclaringType.FullName == @interface.FullName)
            {
                return(true);
            }

            if (method.IsExplicitImplementationOf(@interface))
            {
                return(true);
            }

            bool   hasSameSignatureMethod = false;
            string signature = method.GetMethodSignature();

            foreach (MethodDefinition interfaceMethod in @interface.Methods)
            {
                if (interfaceMethod.GetMethodSignature() == signature)
                {
                    hasSameSignatureMethod = true;
                }
            }
            if (!hasSameSignatureMethod)
            {
                return(false);
            }
            TypeDefinition declaringType = method.DeclaringType.Resolve();

            if (declaringType == null)
            {
                return(false);
            }
            List <TypeDefinition> baseTypes = declaringType.GetBaseTypes();

            foreach (TypeDefinition t in baseTypes)
            {
                if (t.FullName == @interface.FullName)
                {
                    return(true);
                }
            }
            return(false);
        }