コード例 #1
0
        private static void RunHashCodeBenchmark()
        {
            var callInfo = new CallInfo(TargetType, null, Flags.InstanceAnyVisibility, MemberTypes.Field, "name",
                                        new[] { typeof(int), typeof(string) }, null, true);
            var callInfoOther = new CallInfo(typeof(CallInfo), null, Flags.InstanceAnyVisibility, MemberTypes.Field, "other",
                                             new[] { typeof(string) }, null, true);
            var sourceInfo      = SourceInfo.CreateFromType(new { ID = 42, Name = "Test" }.GetType());
            var sourceInfoOther = SourceInfo.CreateFromType(new { id = 42, Name = "Test" }.GetType());

            var initMap   = new Dictionary <string, Action> {
            };
            var actionMap = new Dictionary <string, Action>
            {
                { "CallInfo GetHashCode", () => callInfo.GetHashCode() },
                { "CallInfo Equals Other", () => callInfo.Equals(callInfoOther) },
                { "SourceInfo GetHashCode", () => sourceInfo.GetHashCode() },
                { "SourceInfo Equals Other", () => sourceInfo.Equals(sourceInfoOther) },
                { "string GetHashCode", () => "foo".GetHashCode() },
                { "string Equals", () => "foo".Equals("bar") },
                { "new CallInfo", () => new CallInfo(TargetType, null, Flags.InstanceAnyVisibility, MemberTypes.Field, "name",
                                                     new[] { typeof(int), typeof(string) }, null, true) },
                { "new SourceInfo", () => new SourceInfo(TargetType, new[] { "ID", "Name" }, new[] { typeof(int), typeof(string) }) },
                { "new SourceInfo anon", () => SourceInfo.CreateFromType(new { ID = 42, Name = "Test" }.GetType()) },
            };

            Execute("HashCode Benchmark", initMap, actionMap);
        }
コード例 #2
0
        /// <summary>
        /// Obtains a list of all methods with the given <paramref name="methodName"/> on the given
        /// <paramref name="obj" />, and invokes the best match for the parameters obtained from the
        /// public properties of the supplied <paramref name="sample"/> object.
        /// TryCallMethod is very liberal and attempts to convert values that are not otherwise
        /// considered compatible, such as between strings and enums or numbers, Guids and byte[16], etc.
        /// </summary>
        /// <returns>The value of the invocation.</returns>
        public static object TryCallMethod(this object obj, string methodName, bool mustUseAllParameters, object sample)
        {
            Type sourceType  = sample.GetType();
            var  sourceInfo  = SourceInfo.CreateFromType(sourceType);
            var  paramValues = sourceInfo.GetParameterValues(sample);

            return(obj.TryCallMethod(methodName, mustUseAllParameters, sourceInfo.ParamNames, sourceInfo.ParamTypes, paramValues));
        }
コード例 #3
0
        public void TestParameterHashGenerator_SameTypeShouldGiveIdenticalHash()
        {
            object     source1 = new { Id = 42 };
            SourceInfo sample1 = SourceInfo.CreateFromType(source1.GetType());

            object     source2 = new { Id = 5 };
            SourceInfo sample2 = SourceInfo.CreateFromType(source2.GetType());

            Assert.AreEqual(sample1.GetHashCode(), sample2.GetHashCode());
        }
コード例 #4
0
        /// <summary>
        /// Creates an instance of the given <paramref name="type"/> using the public properties of the
        /// supplied <paramref name="sample"/> object as input.
        /// This method will try to determine the least-cost route to constructing the instance, which
        /// implies mapping as many properties as possible to constructor parameters. Remaining properties
        /// on the source are mapped to properties on the created instance or ignored if none matches.
        /// TryCreateInstance is very liberal and attempts to convert values that are not otherwise
        /// considered compatible, such as between strings and enums or numbers, Guids and byte[], etc.
        /// </summary>
        /// <returns>An instance of <paramref name="type"/>.</returns>
        public static object TryCreateInstance(this Type type, object sample)
        {
            Type       sourceType = sample.GetType();
            SourceInfo sourceInfo = sourceInfoCache.Get(sourceType);

            if (sourceInfo == null)
            {
                sourceInfo = SourceInfo.CreateFromType(sourceType);
                sourceInfoCache.Insert(sourceType, sourceInfo);
            }
            object[]  paramValues = sourceInfo.GetParameterValues(sample);
            MethodMap map         = MapFactory.PrepareInvoke(type, sourceInfo.ParamNames, sourceInfo.ParamTypes, paramValues);

            return(map.Invoke(paramValues));
        }
コード例 #5
0
        private static int[] GetSampleHashes()
        {
            var sources = new object[]
            {
                new { }, new { Id = 42 }, new { Name = "Scar" }, new { Id = 42, Name = "Scar" },
                new { Id = 42, Birthday = DateTime.Now },
                new { Id = 42, Name = "Scar", Birthday = DateTime.Now, Dummy = 0 }
            };
            int index = 0;
            var infos = new SourceInfo[sources.Length];

            Array.ForEach(sources, s => { infos[index++] = SourceInfo.CreateFromType(s.GetType()); });
            index = 0;
            int[] hashes = new int[sources.Length];
            Array.ForEach(infos, i => { hashes[index++] = i.GetHashCode(); });
            return(hashes);
        }