Пример #1
0
        /// <summary>
        /// Gets the mapped method.
        /// </summary>
        /// <param name="typeOfTheSubject">The type of the subject.</typeparam>
        /// <param name="name">The method name.</param>
        /// <param name="argTypes">The arguments passed in to the method.</param>
        /// <param name="genericArgs">The generic argument types.</param>
        /// <returns>
        /// The mapped method
        /// </returns>
        public MappedMethod GetMappedMethod(Type typeOfTheSubject, string name, Type[] argTypes, params Type[] genericArgs)
        {
            argTypes    = argTypes ?? Type.EmptyTypes;
            genericArgs = genericArgs ?? Type.EmptyTypes;

            Contract.Assume(!string.IsNullOrEmpty(name));
            IMethodMapping mapping = this.Lookup(name, argTypes, genericArgs);

            if (mapping == default(IMethodMapping))
            {
                Fasterflect.MethodInvoker invoker = typeOfTheSubject.GetMethod(name, argTypes, genericArgs);

                // TODO: Abstract IMethodMapping creation?
                mapping = new MethodMapping()
                {
                    Name                 = name,
                    ArgumentTypes        = argTypes,
                    GenericArgumentTypes = genericArgs ?? Type.EmptyTypes,
                    Subject              = (subject, args) => invoker(subject, args)
                };

                this.Add(mapping);
            }

            MappedMethod method = (args) =>
            {
                return(mapping.Subject(this.Subject, args));
            };

            return(method);
        }
Пример #2
0
        /// <summary>
        /// Adds the specified mapping.
        /// </summary>
        /// <param name="mapping">The mapping.</param>
        /// <param name="replace">if set to <c>true</c> [replaces an IMethodMapping instance, if there is one that matches the signature].</param>
        public void Add(IMethodMapping mapping, bool replace = false)
        {
            Contract.Ensures(this.Contains(mapping));

            if (replace)
            {
                this.Remove(mapping);
            }

            this.mappings.Add(mapping);
        }
Пример #3
0
        /// <summary>
        /// Removes the specified mapping.
        /// </summary>
        /// <param name="mapping">The mapping.</param>
        /// <returns>
        /// true if removed succesfully, false if it wasn't contained in this IInterfaceMap
        /// </returns>
        public bool Remove(IMethodMapping mapping)
        {
            Contract.Ensures(!this.Contains(mapping));

            IMethodMapping old = this.Lookup(mapping.Name, mapping.ArgumentTypes.ToArray(), mapping.GenericArgumentTypes.ToArray());

            if (old != default(IMethodMapping))
            {
                return(this.mappings.Remove(old));
            }

            return(false);
        }
        public void CanRemoveNonContained()
        {
            // Arrange
            IInterfaceMap  target  = CreateIInterfaceMap();
            IMethodMapping mapping = CreateIMethodMapping();

            mapping.Name = "test";
            mapping.GenericArgumentTypes = Type.EmptyTypes;
            mapping.ArgumentTypes        = Type.EmptyTypes;
            bool actual;

            // Act
            actual = target.Remove(mapping);

            // Assert
            Verify.That(actual).IsFalse()
            .Now();
        }
        public void CanAddWithReplaceSetToFalse()
        {
            // Arrange
            IInterfaceMap  target  = CreateIInterfaceMap();
            IMethodMapping mapping = CreateIMethodMapping();

            mapping.Name = "test";
            mapping.GenericArgumentTypes = new Type[] { typeof(string), typeof(object) };
            mapping.ArgumentTypes        = new Type[] { typeof(bool) };
            mapping.Subject = (arg, args) =>
            {
                Console.Out.WriteLine("Subject called");
                return(true);
            };
            bool replace = false;

            // Act
            target.Add(mapping, replace);

            // Assert
            Verify.That(target.AsEnumerable()).DoesContain(mapping)
            .Now();
        }
        public void CanReplaceAMethodMapping()
        {
            // Arrange
            IInterfaceMap target = CreateIInterfaceMap();

            IMethodMapping mapping = CreateIMethodMapping();

            mapping.Name = "test";
            mapping.GenericArgumentTypes = new Type[] { typeof(string), typeof(object) };
            mapping.ArgumentTypes        = new Type[] { typeof(bool) };
            mapping.Subject = (arg, args) =>
            {
                Console.Out.WriteLine("Subject called");
                return(true);
            };
            IMethodMapping replacement = CreateIMethodMapping();

            replacement.Name = "test";
            replacement.GenericArgumentTypes = new Type[] { typeof(string), typeof(object) };
            replacement.ArgumentTypes        = new Type[] { typeof(bool) };
            replacement.Subject = (arg, args) =>
            {
                Console.Out.WriteLine("Subject replacement called");
                return(true);
            };

            // Act
            target.Add(mapping, false);
            target.Add(replacement, true);

            // Assert
            Verify.That(target.AsEnumerable()).DoesContain(replacement, "Should contain the replacement")
            .Now();
            Verify.That(target.AsEnumerable()).DoesNotContain(mapping, "Should not contain the original mapping")
            .Now();
        }
Пример #7
0
 public bool Remove(IMethodMapping mapping)
 {
     Contract.Requires(mapping != null, "mapping is null.");
     return(default(bool));
 }
Пример #8
0
 public void Add(IMethodMapping mapping, bool replace = false)
 {
     Contract.Requires(mapping != null, "mapping is null.");
 }