Esempio n. 1
0
 public GetCharsGenerator()
 {
     SupportedMethods = new[]
     {
         ReflectHelper.GetMethod <string, char>(s => s[0])
     };
 }
Esempio n. 2
0
 public NewGuidHqlGenerator()
 {
     SupportedMethods = new[]
     {
         ReflectHelper.GetMethod(() => Guid.NewGuid())
     };
 }
Esempio n. 3
0
 protected override Expression VisitConstant(ConstantExpression node)
 {
     if (node.Type == typeof(TimeSpan))
     {
         return(Expression.Call(ReflectHelper.GetMethod(() => LinqExtensionMethods.MappedAs <object>(null, null)).GetGenericMethodDefinition().MakeGenericMethod(typeof(TimeSpan)), node, Expression.Constant(NHibernateUtil.TimeSpan)));
     }
     return(base.VisitConstant(node));
 }
Esempio n. 4
0
        public EntityBatchFetcher(AbstractEntityPersister persister)
        {
            _parameter = Expression.Parameter(typeof(TEntity));
            var property = Expression.Property(_parameter, persister.IdentifierPropertyName);

            _idExpression   = Expression.Lambda <Func <TEntity, TId> >(property, _parameter);
            _idSelector     = Expression.Lambda <Func <TEntity, object> >(Expression.Convert(property, typeof(object)), _parameter).Compile();
            _containsMethod = ReflectHelper.GetMethod(() => Enumerable.Contains(null, default(TId)));
        }
Esempio n. 5
0
        public void BadOverload()
        {
            var sumMethodTemplate = ReflectHelper.GetMethod(() => Queryable.Sum((IQueryable <int>)null));

            Assert.Throws <InvalidOperationException>(() =>
            {
                ReflectHelper.GetMethodOverload(sumMethodTemplate, typeof(object));
            });
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
 public RandomHqlGenerator()
 {
     SupportedMethods = new[]
     {
         _nextDouble,
         ReflectHelper.GetMethod <Random>(r => r.Next()),
         ReflectHelper.GetMethod <Random>(r => r.Next(2)),
         ReflectHelper.GetMethod <Random>(r => r.Next(-1, 1))
     };
 }
 public EntityWithUserTypePropertyGeneratorsRegistry()
 {
     RegisterGenerator(ReflectHelper.GetMethod((IExample e) => e.IsEquivalentTo(null)),
                       new EntityWithUserTypePropertyIsEquivalentGenerator());
 }
Esempio n. 9
0
 /// <summary>
 /// Extract the <see cref="MethodInfo"/> from a given expression.
 /// </summary>
 /// <param name="method">The method.</param>
 /// <returns>The <see cref="MethodInfo"/> of the method.</returns>
 public static MethodInfo GetMethod(Expression <System.Action> method)
 {
     return(ReflectHelper.GetMethod(method));
 }
Esempio n. 10
0
 /// <summary>
 /// Extract the <see cref="MethodInfo"/> from a given expression.
 /// </summary>
 /// <typeparam name="TSource">The declaring-type of the method.</typeparam>
 /// <param name="method">The method.</param>
 /// <returns>The <see cref="MethodInfo"/> of the method.</returns>
 public static MethodInfo GetMethod <TSource>(Expression <Action <TSource> > method)
 {
     return(ReflectHelper.GetMethod(method));
 }
Esempio n. 11
0
        public void ReflectionBluntPerfCompare()
        {
            var swCached = new Stopwatch();

            swCached.Start();
            for (var i = 0; i < 1000; i++)
            {
                Trace.TraceInformation(CastMethod.ToString());
            }
            swCached.Stop();

            var swCachedDef = new Stopwatch();

            swCachedDef.Start();
            for (var i = 0; i < 1000; i++)
            {
                var cast = CastMethodDefinition.MakeGenericMethod(new[] { typeof(int) });
                Trace.TraceInformation(cast.ToString());
            }
            swCachedDef.Stop();

            var swRefl2 = new Stopwatch();

            swRefl2.Start();
            for (var i = 0; i < 1000; i++)
            {
                var cast = GetMethod2(Enumerable.Cast <int>, (IEnumerable <int>)null);
                Trace.TraceInformation(cast.ToString());
            }
            swRefl2.Stop();

            var swRefl2Def = new Stopwatch();

            swRefl2Def.Start();
            for (var i = 0; i < 1000; i++)
            {
                var cast = GetMethodDefinition2(Enumerable.Cast <object>, (IEnumerable <object>)null)
                           .MakeGenericMethod(new[] { typeof(int) });
                Trace.TraceInformation(cast.ToString());
            }
            swRefl2Def.Stop();

            var swRefl = new Stopwatch();

            swRefl.Start();
            for (var i = 0; i < 1000; i++)
            {
                var cast = ReflectHelper.GetMethod(() => Enumerable.Cast <int>(null));
                Trace.TraceInformation(cast.ToString());
            }
            swRefl.Stop();

            var swReflDef = new Stopwatch();

            swReflDef.Start();
            for (var i = 0; i < 1000; i++)
            {
                var cast = ReflectHelper.GetMethodDefinition(() => Enumerable.Cast <object>(null))
                           .MakeGenericMethod(new[] { typeof(int) });
                Trace.TraceInformation(cast.ToString());
            }
            swReflDef.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 cast = EnumerableHelper.GetMethod("Cast", new[] { typeof(IEnumerable) }, new[] { typeof(int) });
#pragma warning restore 0618
                Trace.TraceInformation(cast.ToString());
            }
            swEnHlp.Stop();

            Assert.Pass(@"Blunt perf timings:
Cached method: {0}
Cached method definition + make gen: {1}
Hazzik GetMethod: {5}
Hazzik GetMethodDefinition + make gen: {6}
ReflectHelper.GetMethod: {2}
ReflectHelper.GetMethodDefinition + make gen: {3}
EnumerableHelper.GetMethod(generic overload): {4}",
                        swCached.Elapsed, swCachedDef.Elapsed, swRefl.Elapsed, swReflDef.Elapsed, swEnHlp.Elapsed,
                        swRefl2.Elapsed, swRefl2Def.Elapsed);
        }
Esempio n. 12
0
        public static SupportedQueryMember ForMethod <T, TValue>(Expression <Func <T, TValue> > methodExpression, IHqlMemberTransformer transformer)
        {
            var method = ReflectHelper.GetMethod(methodExpression);

            return(new SupportedQueryMember(method, transformer));
        }