コード例 #1
0
        public void ShouldImplementIMethodReplacementHostOnTargetType()
        {
            Type modifiedTargetType = GetModifiedTargetType();

            var instance = Activator.CreateInstance(modifiedTargetType);
            Assert.IsNotNull(instance);
            Assert.IsTrue(instance is IMethodReplacementHost);

            IMethodReplacementHost host = (IMethodReplacementHost)instance;

            var interceptor = new SampleMethodReplacement();

            host.MethodCallReplacementProvider = new SampleMethodReplacementProvider(interceptor);

            MethodInfo targetMethod = modifiedTargetType.GetMethod("DoSomething");
            try
            {
                targetMethod.Invoke(instance, null);
            }
            catch (TargetInvocationException ex)
            {
                var innerException = ex.InnerException;
                Console.WriteLine(ex.ToString());
                throw innerException;
            }

            Assert.IsTrue(interceptor.HasBeenCalled);
        }
コード例 #2
0
        public void ShouldInvokeClassMethodReplacementProviderIfInterceptionIsEnabled()
        {
            Func<MethodReference, bool> methodFilter = m => m.Name == "DoSomething";
            var replacement = new SampleMethodReplacement();
            var provider = new SampleMethodReplacementProvider(replacement);

            MethodBodyReplacementProviderRegistry.SetProvider(provider);
            Action<Type> doTest = type =>
            {
                var doSomethingMethod = type.GetMethod("DoSomething");
                Assert.IsNotNull(doSomethingMethod);

                doSomethingMethod.Invoke(null, new object[0]);
            };

            Test("SampleLibrary.dll", "SampleStaticClassWithStaticMethod", methodFilter, doTest);
            Assert.IsTrue(replacement.HasBeenCalled);
        }