Esempio n. 1
0
        public void BadOverload()
        {
            var sumMethodTemplate = ReflectHelper.GetMethod(() => Queryable.Sum((IQueryable <int>)null));

            Assert.Throws <InvalidOperationException>(() =>
            {
                ReflectHelper.GetMethodOverload(sumMethodTemplate, typeof(object));
            });
        }
Esempio n. 2
0
        public void OverloadReflectionBluntPerfCompare()
        {
            var sumMethodTemplate = ReflectHelper.GetMethod(() => Queryable.Sum((IQueryable <int>)null));

            var swNoSameParamsCheck = new Stopwatch();

            swNoSameParamsCheck.Start();
            for (var i = 0; i < 1000; i++)
            {
                var sumMethod = sumMethodTemplate.DeclaringType.GetMethod(sumMethodTemplate.Name,
                                                                          (sumMethodTemplate.IsStatic ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.Public,
                                                                          null, new[] { typeof(IQueryable <decimal>) }, null);
                Trace.TraceInformation(sumMethod.ToString());
            }
            swNoSameParamsCheck.Stop();

            var swCurrentChoiceSameType = new Stopwatch();

            swCurrentChoiceSameType.Start();
            for (var i = 0; i < 1000; i++)
            {
                var sumMethod = ReflectHelper.GetMethodOverload(sumMethodTemplate, typeof(IQueryable <int>));
                Trace.TraceInformation(sumMethod.ToString());
            }
            swCurrentChoiceSameType.Stop();

            var swCurrentChoice = new Stopwatch();

            swCurrentChoice.Start();
            for (var i = 0; i < 1000; i++)
            {
                var sumMethod = ReflectHelper.GetMethodOverload(sumMethodTemplate, typeof(IQueryable <long>));
                Trace.TraceInformation(sumMethod.ToString());
            }
            swCurrentChoice.Stop();

            var swEnHlp = new Stopwatch();

            swEnHlp.Start();
            for (var i = 0; i < 1000; i++)
            {
                // Testing the obsolete helper perf. Disable obsolete warning. Remove this swEnHlp part of the test if this helper is to be removed.
#pragma warning disable 0618
                var sumMethod = EnumerableHelper.GetMethod("Sum", new[] { typeof(IEnumerable <int>) });
#pragma warning restore 0618
                Trace.TraceInformation(sumMethod.ToString());
            }
            swEnHlp.Stop();

            Assert.Pass(@"Blunt perf timings:
Direct reflection: {0}
Current impl, same overload: {1}
Current impl, other overload: {2}
EnumerableHelper.GetMethod(non generic overload): {3}",
                        swNoSameParamsCheck.Elapsed, swCurrentChoiceSameType.Elapsed, swCurrentChoice.Elapsed, swEnHlp.Elapsed);
        }
        private MethodCallExpression GetAggregateMethodCall(MethodInfo aggregateMethodTemplate, System.Type inputListType,
                                                            System.Type elementType, Expression inputList)
        {
            MethodInfo aggregateMethod;

            if (aggregateMethodTemplate.IsGenericMethodDefinition)
            {
                aggregateMethod = aggregateMethodTemplate.MakeGenericMethod(elementType);
            }
            else
            {
                // Ensure we use the right overload.
                aggregateMethod = ReflectHelper.GetMethodOverload(aggregateMethodTemplate, inputListType);
            }
            return(Expression.Call(aggregateMethod, inputList));
        }