Ejemplo n.º 1
0
        public static void Isolate(Action entryPoint, params IShims[] shims)
        {
            if (shims == null || shims.Length == 0)
            {
                entryPoint.Invoke();
                return;
            }

            ShimCollection shms = new ShimCollection();

            foreach (IShims shim in shims)
            {
                foreach (var s in shim.GetShims())
                {
                    shms.Add(s);
                }
            }

            Shims     = shms.ToArray();
            StubCache = new Dictionary <MethodBase, DynamicMethod>();

            Type           delegateType = typeof(Action <>).MakeGenericType(entryPoint.Target.GetType());
            MethodRewriter rewriter     = MethodRewriter.CreateRewriter(entryPoint.Method);

            ((MethodInfo)(rewriter.Rewrite())).CreateDelegate(delegateType).DynamicInvoke(entryPoint.Target);
        }
Ejemplo n.º 2
0
        public void TestConstructorRewrite()
        {
            List <string>   list            = new List <string>();
            ConstructorInfo constructorInfo = typeof(List <string>).GetConstructor(Type.EmptyTypes);
            MethodRewriter  methodRewriter  = MethodRewriter.CreateRewriter(constructorInfo, false);
            DynamicMethod   dynamicMethod   = methodRewriter.Rewrite() as DynamicMethod;

            Assert.AreEqual(typeof(void), dynamicMethod.ReturnType);
            Assert.AreEqual(typeof(List <string>), dynamicMethod.GetParameters()[0].ParameterType);
        }
Ejemplo n.º 3
0
        public void TestStaticMethodRewrite()
        {
            MethodInfo     methodInfo     = typeof(DateTime).GetMethod("get_Now");
            MethodRewriter methodRewriter = MethodRewriter.CreateRewriter(methodInfo, false);
            DynamicMethod  dynamicMethod  = methodRewriter.Rewrite() as DynamicMethod;

            Delegate func = dynamicMethod.CreateDelegate(typeof(Func <DateTime>));

            Assert.AreEqual(DateTime.Now.ToString("yyyyMMdd_HHmm"), ((DateTime)func.DynamicInvoke()).ToString("yyyyMMdd_HHmm"));
        }
Ejemplo n.º 4
0
        public void TestStaticMethodRewrite()
        {
            MethodInfo     methodInfo     = typeof(StubHelper).GetMethod("GetMethodPointer");
            MethodRewriter methodRewriter = MethodRewriter.CreateRewriter(methodInfo);
            DynamicMethod  dynamicMethod  = methodRewriter.Rewrite() as DynamicMethod;

            Delegate func = dynamicMethod.CreateDelegate(typeof(Func <MethodBase, IntPtr>));

            Assert.AreEqual(StubHelper.GetMethodPointer(methodInfo), func.DynamicInvoke(methodInfo));
        }
Ejemplo n.º 5
0
        public void TestExceptionHandlersRewrite()
        {
            MethodInfo     methodInfo     = typeof(MethodRewriterTests).GetMethod("ExceptionHandlersRewriteMethod");
            MethodRewriter methodRewriter = MethodRewriter.CreateRewriter(methodInfo);
            DynamicMethod  dynamicMethod  = methodRewriter.Rewrite() as DynamicMethod;

            Delegate func = dynamicMethod.CreateDelegate(typeof(Func <int>));

            Assert.AreEqual(1, (int)func.DynamicInvoke());
        }
Ejemplo n.º 6
0
        public static void Isolate(Action entryPoint, params Shim[] shims)
        {
            if (shims == null || shims.Length == 0)
            {
                entryPoint.Invoke();
                return;
            }

            Shims = shims;
            Type           delegateType = typeof(Action <>).MakeGenericType(entryPoint.Target.GetType());
            MethodRewriter rewriter     = MethodRewriter.CreateRewriter(entryPoint.Method);

            ((MethodInfo)(rewriter.Rewrite())).CreateDelegate(delegateType).DynamicInvoke(entryPoint.Target);
        }
Ejemplo n.º 7
0
        public void TestInstanceMethodRewrite()
        {
            string         item           = "Item 1";
            List <string>  list           = new List <string>();
            MethodInfo     methodInfo     = typeof(List <string>).GetMethod("Add");
            MethodRewriter methodRewriter = MethodRewriter.CreateRewriter(methodInfo, false);
            DynamicMethod  dynamicMethod  = methodRewriter.Rewrite() as DynamicMethod;

            Delegate func = dynamicMethod.CreateDelegate(typeof(Action <List <string>, string>));

            func.DynamicInvoke(list, item);

            Assert.AreEqual(1, list.Count);
            Assert.AreEqual(item, list[0]);
        }
Ejemplo n.º 8
0
        public static T IsolateDelegate <T>(Delegate entryPoint, Type delegateType, Shim[] shims, params object[] args)
        {
            var returnType = typeof(T);

            if (entryPoint.Method.ReturnType != returnType)
            {
                throw new InvalidOperationException("Cannot return a type of " + returnType + " when specified method expects " + entryPoint.Method.ReturnType + ".");
            }

            if (shims == null || shims.Length == 0)
            {
                return((T)entryPoint.DynamicInvoke(args));
            }

            Shims     = shims;
            StubCache = new Dictionary <MethodBase, DynamicMethod>();

            MethodRewriter rewriter    = MethodRewriter.CreateRewriter(entryPoint.Method);
            var            delToInvoke = ((MethodInfo)(rewriter.Rewrite())).CreateDelegate(delegateType, entryPoint.Target);

            return((T)delToInvoke.DynamicInvoke(args));
        }
Ejemplo n.º 9
0
        public static void IsolateDelegate(Delegate entryPoint, Type delegateType, Shim[] shims, params object[] args)
        {
            if (shims == null || shims.Length == 0)
            {
                entryPoint.DynamicInvoke();
                return;
            }

            Shims     = shims;
            StubCache = new Dictionary <MethodBase, DynamicMethod>();

            MethodRewriter rewriter    = MethodRewriter.CreateRewriter(entryPoint.Method);
            var            delToInvoke = ((MethodInfo)(rewriter.Rewrite())).CreateDelegate(delegateType);

            if (IsActionDelegateWithoutEntryPointFirstParam(delToInvoke, entryPoint.Method) ||
                entryPoint.Target == null)
            {
                delToInvoke.DynamicInvoke(args);
            }
            else
            {
                delToInvoke.DynamicInvoke(entryPoint.Target);
            }
        }