Пример #1
0
        /// <summary>
        /// Lookups the specified method mapping.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="argumentTypes">The argument types.</param>
        /// <param name="genericArgumentTypes">The generic argument types.</param>
        /// <returns>The MethodMapping</returns>
        private IMethodMapping Lookup(string methodName, Type[] argumentTypes, params Type[] genericArgumentTypes)
        {
            Contract.Requires(!String.IsNullOrEmpty(methodName), "name is null or empty.");
            Contract.Requires(argumentTypes != null, "argTypes is null.");

            genericArgumentTypes = genericArgumentTypes ?? Type.EmptyTypes;

            return(this.mappings.FirstOrDefault(mapping =>
            {
                bool matches = mapping.Name == methodName;
                if (matches)
                {
                    matches =
                        comparer.GetHashCode(argumentTypes) == comparer.GetHashCode(mapping.ArgumentTypes.ToArray()) ||
                        comparer.Equals(argumentTypes, mapping.ArgumentTypes.ToArray());
                    if (matches)
                    {
                        matches =
                            comparer.GetHashCode(genericArgumentTypes) == comparer.GetHashCode(mapping.GenericArgumentTypes.ToArray()) ||
                            comparer.Equals(argumentTypes, mapping.GenericArgumentTypes.ToArray());
                    }
                }

                return matches;
            }));
        }
Пример #2
0
        public void GetHashCodeTest()
        {
            TypeArrayComparer target = new TypeArrayComparer();

            Type[] x    = new Type[] { typeof(string), typeof(Buffer), typeof(WeakReference) };
            Type[] xRef = x;
            Type[] y    = new Type[] { typeof(string), typeof(Buffer), typeof(WeakReference) };
            Type[] z    = new Type[] { typeof(OperatingSystem), typeof(Buffer), typeof(WeakReference) };

            int xHashCode    = target.GetHashCode(x);
            int xRefHashCode = target.GetHashCode(xRef);
            int yHashCode    = target.GetHashCode(y);
            int zHashCode    = target.GetHashCode(z);

            Console.Out.WriteLine("HashCodes:\n\tx:{0}  xRef:{1}  y:{2}  z:{3}", xHashCode, xRefHashCode, yHashCode, zHashCode);
            Assert.AreEqual(xHashCode, xRefHashCode, "reference of a Type[] should be equal to another reference to the same Type[]");
            Assert.AreEqual(target.GetHashCode(x), yHashCode, "Two arrays with the same elements, should be equal.");
            Assert.AreNotEqual(target.GetHashCode(y), target.GetHashCode(z), "Two arrays with different elements should be different.");
            Assert.AreNotEqual(target.GetHashCode(x), zHashCode, "Two arrays with different elements should be different.");
        }