Esempio n. 1
0
        private static MethodReference GetImplementedMethodFromInterface(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.");
            }

            TypeDefinition declaringType = method.DeclaringType.Resolve();

            if (declaringType == null)
            {
                return(null);
            }

            bool isDeclaringTypeImplementingInterface = false;
            List <TypeDefinition> baseTypes           = declaringType.GetBaseTypes();

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

            if (!isDeclaringTypeImplementingInterface)
            {
                return(null);
            }

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

            MethodReference explictlyImplementedMethod = method.GetExplicitlyImplementedMethodFromInterface(@interface);

            if (explictlyImplementedMethod != null)
            {
                return(explictlyImplementedMethod);
            }

            foreach (MethodDefinition interfaceMethod in @interface.Methods)
            {
                if (method.HasSameSignatureWith(interfaceMethod))
                {
                    return(interfaceMethod);
                }
            }

            return(null);
        }