コード例 #1
0
ファイル: PoseContext.cs プロジェクト: jonkeda/pose
        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);
        }
コード例 #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);
        }
コード例 #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"));
        }
コード例 #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));
        }
コード例 #5
0
ファイル: MethodRewriterTests.cs プロジェクト: vCipher/pose
        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());
        }
コード例 #6
0
ファイル: PoseContext.cs プロジェクト: jonthegiant/pose
        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);
        }
コード例 #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]);
        }
コード例 #8
0
ファイル: PoseContext.cs プロジェクト: frogsquire/shimmy
        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));
        }
コード例 #9
0
        private static ITypeRewriter CreateTypeRewriter
        (
            AssemblyDefinition mscorlib,
            TypeDefinition bindingsBaseType,
            bool useDllImport
        )
        {
            var methodProcessors = new IMethodProcessor[]
            {
                new ParameterProcessor(mscorlib, bindingsBaseType),
                new NativeCallProcessor(useDllImport),
                new ReturnTypeProcessor(mscorlib),
            };

            if (Options.EnableDebugCalls)
            {
                methodProcessors = methodProcessors.Prepend(new DebugPrologueProcessor(mscorlib)).ToArray();
            }

            var methodRewriter = new MethodRewriter(methodProcessors);

            return(new TypeRewriter(mscorlib, useDllImport, methodRewriter));
        }
コード例 #10
0
ファイル: PoseContext.cs プロジェクト: frogsquire/shimmy
        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);
            }
        }