Beispiel #1
0
        private void LateBind()
        {
            Type staticType = Type.GetType("Microsoft.Practices.Unity.UnityContainerExtensions, " + _containerInterface.Assembly.FullName, true);

            _unityContainerExtensions = new ImpromptuLateLibraryType(staticType);
            _staticContext            = InvokeContext.CreateStatic(staticType);
        }
Beispiel #2
0
        public void CacheableGetStaticTimed()
        {
            var tStaticType   = typeof(DateTime);
            var tContext      = InvokeContext.CreateStatic(tStaticType);
            var tCachedInvoke = new CacheableInvocation(InvocationKind.Get, "Today", context: tContext);

            Timer.Action1 = (() =>
            {
                var tOut = tCachedInvoke.Invoke(tStaticType);
            });
            var tMethodInfo = typeof(DateTime).GetProperty("Today").GetGetMethod();

            Timer.Action2 = (() =>
            {
                var tOut = tMethodInfo.Invoke(tStaticType, new object[] { });
            });


            var elapsed = Timer.Go(3 * TimeIt.Million);

            Console.WriteLine("Impromptu: " + elapsed.Item1);
            Console.WriteLine("Refelection: " + elapsed.Item2);
            Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed));
            Assert.Less(elapsed.Item1, elapsed.Item2);
        }
Beispiel #3
0
        private void LateBind()
        {
            Type type       = _kernelInterface;
            Type staticType = Type.GetType("Ninject.ResolutionExtensions, " + type.Assembly.FullName, true);

            _staticContext        = InvokeContext.CreateStatic(staticType);
            _resolutionExtensions = new ImpromptuLateLibraryType(staticType);
        }
Beispiel #4
0
        private static bool TryHandleSingleton(Type type, out object instance)
        {
            var properties      = type.GetProperties();
            var desiredProperty = properties.FirstOrDefault(x => x.Name == "Instance" && x.PropertyType == type);

            if (desiredProperty == null)
            {
                instance = null;
                return(false);
            }
            instance = Dynamic.InvokeGet(InvokeContext.CreateStatic(type), desiredProperty.Name);
            // instance = desiredProperty.GetGetMethod().Invoke(null, Type.EmptyTypes);
            return(true);
        }
Beispiel #5
0
        public void MethodStaticMethodValueTimed()
        {
#if DEBUG
            Assert.Ignore("Visual Studio slows down dynamic too much in debug mode");
#endif
            var    tStaticType = typeof(DateTime);
            var    tTarget     = InvokeContext.CreateStatic(tStaticType);
            string tDate       = "01/20/2009";
            Timer.Action1 = (() => { var tOut = Dynamic.InvokeMember(tTarget, "Parse", tDate); });
            var tMethodInfo = typeof(DateTime).GetMethod("Parse", new[] { typeof(string) });
            Timer.Action2 = (() =>
            {
                var tOut = tMethodInfo.Invoke(tStaticType, new object[] { tDate });
            });

            var elapsed = Timer.Go();

            Console.WriteLine("Impromptu: " + elapsed.Item1);
            Console.WriteLine("Refelection: " + elapsed.Item2);
            Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed));
            Assert.Less(elapsed.Item1, elapsed.Item2);
        }
Beispiel #6
0
        public void GetStaticTimed()
        {
#if DEBUG
            Assert.Ignore("Visual Studio slows down dynamic too much in debug mode");
#endif


            var tStaticType = typeof(DateTime);
            var tTarget     = InvokeContext.CreateStatic(tStaticType);
            Timer.Action1 = (() => { var tOut = Dynamic.InvokeGet(tTarget, "Today"); });
            var tMethodInfo = typeof(DateTime).GetProperty("Today").GetGetMethod();
            Timer.Action2 = (() =>
            {
                var tOut = tMethodInfo.Invoke(tStaticType, new object[] { });
            });

            var elapsed = Timer.Go(3 * TimeIt.Million);

            Console.WriteLine("Impromptu: " + elapsed.Item1);
            Console.WriteLine("Refelection: " + elapsed.Item2);
            Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed));
            Assert.Less(elapsed.Item1, elapsed.Item2);
        }
Beispiel #7
0
        public void CacheableMethodStaticMethodValueTimed()
        {
            var    tStaticType = typeof(DateTime);
            var    tContext    = InvokeContext.CreateStatic(tStaticType);
            string tDate       = "01/20/2009";

            var tCachedInvoke = new CacheableInvocation(InvocationKind.InvokeMember, "Parse", argCount: 1,
                                                        context: tContext);

            Timer.Action1 = (() => { var tOut = tCachedInvoke.Invoke(tStaticType, tDate); });
            var tMethodInfo = typeof(DateTime).GetMethod("Parse", new[] { typeof(string) });

            Timer.Action2 = (() =>
            {
                var tOut = tMethodInfo.Invoke(tStaticType, new object[] { tDate });
            });

            var elapsed = Timer.Go();

            Console.WriteLine("Impromptu: " + elapsed.Item1);
            Console.WriteLine("Refelection: " + elapsed.Item2);
            Console.WriteLine("Impromptu VS Reflection: {0}", TimeIt.RelativeSpeed(elapsed));
            Assert.Less(elapsed.Item1, elapsed.Item2);
        }
 public StaticDynamicWrapper(Type target) : base(InvokeContext.CreateStatic(target))
 {
 }
Beispiel #9
0
        /// <summary>
        /// Visit a method call expression
        /// </summary>
        /// <param name="expCall"></param>
        /// <returns></returns>
        protected EToken VisitMethodCallExpression(MethodCallExpression expCall)
        {
            List <EToken> args = (from p in expCall.Arguments
                                  select this.VisitExpression(p, expCall)).ToList();

            EToken expCallTargetExpression;

            if (expCall.Object == null)
            {
                // Here we should use FullName of declaring type, but a bug in Dynamic.Linq.Core prevents
                // usage of full namespaces and uses shortcut aliases only.
                // TODO: El tipo ExpressionType.Unbox aquí es incorrecto, debería ser null
                expCallTargetExpression = new EToken(expCall.Method?.DeclaringType?.Name, ExpressionType.Unbox, null);
            }
            else
            {
                // Métodos en objetos
                expCallTargetExpression = this.VisitExpression(expCall.Object, expCall);
            }

            bool canMaterializeMethodCall = args.All((i) => i.Type == ExpressionType.Constant) &&
                                            (expCallTargetExpression.Type == ExpressionType.Constant ||
                                             expCallTargetExpression.Type == ExpressionType.Unbox);

            if (canMaterializeMethodCall)
            {
                List <object> methodCallUnboxedArgs = new List <object>();

                // Grab all arguments and remove them from the dictionary
                foreach (var arg in args)
                {
                    methodCallUnboxedArgs.Add(this.PopArgument(arg.Text));
                }

                object expCallResult;

                if (expCall.Object == null)
                {
                    expCallResult = Dynamic.InvokeMember(
                        InvokeContext.CreateStatic(expCall.Method?.DeclaringType),
                        expCall.Method.Name,
                        methodCallUnboxedArgs.ToArray());
                }
                else
                {
                    expCallResult = Dynamic.InvokeMember(
                        this.PopArgument(expCallTargetExpression.Text),
                        expCall.Method.Name,
                        methodCallUnboxedArgs.ToArray());
                }

                return(this.CheckMaterializationResult(expCallResult, expCall.Method.ReturnType));
            }

            // If this method is LinqKit's Invoke() then we can do this ASAP because Dynamic.Linq.Core won't swallow complex method calls
            // We asume that the coder is looking for lazy evaluation
            if (expCall.Method.Name == "Invoke" && expCall.Method?.DeclaringType?.FullName.Contains("LinqKit") == true)
            {
                var targetOfInvoke = expCall.Arguments.First() as MethodCallExpression;

                var argumentsOfTargetInvoke = new List <object>();
                foreach (var arg in targetOfInvoke.Arguments)
                {
                    if (arg is ConstantExpression constant)
                    {
                        argumentsOfTargetInvoke.Add(constant.Value);
                    }
                }

                // Materialize it! Currently only works with static calls
                var materializedTargetOfInvoke = Dynamic.InvokeMember(
                    InvokeContext.CreateStatic(targetOfInvoke.Method?.DeclaringType),
                    targetOfInvoke.Method.Name,
                    argumentsOfTargetInvoke.ToArray()) as LambdaExpression;

                var result = this.VisitLambdaExpression(materializedTargetOfInvoke);

                // Replace the target source arguments
                for (int x = 0; x < materializedTargetOfInvoke.Parameters.Count; x++)
                {
                    var expArg      = materializedTargetOfInvoke.Parameters[x];
                    var externalArg = args[x + 1];

                    this.Parameters.Remove(expArg.Name);
                    this.TempParameters.Remove(expArg.Name);
                    result.Text = result.Text.Replace($"[{expArg.Name}]", externalArg.Text);
                }

                return(result);
            }

            return(new EToken($"{expCallTargetExpression}.{expCall.Method.Name}({args.StringJoinObject(", ")})", expCall.NodeType, expCall.Method.ReturnType));
        }