private MethodInfo GetOrCreateImplementationMethod(MutableType declaringType, MethodInfo ifcMethod, out bool isNewlyCreated)
        {
            var interfaceMap   = declaringType.GetInterfaceMap(ifcMethod.DeclaringType, allowPartialInterfaceMapping: true);
            var index          = Array.IndexOf(interfaceMap.InterfaceMethods, ifcMethod);
            var implementation = interfaceMap.TargetMethods[index];

            if (implementation != null)
            {
                isNewlyCreated = false;
                return(implementation);
            }
            isNewlyCreated = true;

            try
            {
                return(CreateMethod(declaringType, ifcMethod, ifcMethod.Name, ifcMethod.Attributes, bodyProvider: null));
            }
            catch (InvalidOperationException)
            {
                var message = string.Format(
                    "Interface method '{0}' cannot be implemented because a method with equal name and signature already exists. "
                    + "Use AddExplicitOverride to create an explicit implementation.",
                    ifcMethod.Name);
                throw new InvalidOperationException(message);
            }
        }
Esempio n. 2
0
        private void CheckGetInterfaceMap(MutableType mutableType, MethodInfo interfaceMethod, MethodInfo expectedImplementationMethod)
        {
            var interfaceType = interfaceMethod.DeclaringType;

            Assertion.IsNotNull(interfaceType);
            Assert.That(interfaceType.IsInterface, Is.True);

            var mapping = mutableType.GetInterfaceMap(interfaceType);

            Assert.That(mapping.InterfaceType, Is.SameAs(interfaceType));
            Assert.That(mapping.TargetType, Is.SameAs(mutableType));
            var interfaceMethodIndex = Array.IndexOf(mapping.InterfaceMethods, interfaceMethod);
            var targetMethodIndex    = Array.IndexOf(mapping.TargetMethods, expectedImplementationMethod);

            Assert.That(targetMethodIndex, Is.EqualTo(interfaceMethodIndex).And.Not.EqualTo(-1));
        }