public void Replace(MethodDefinition targetMethod) { var replacer = new MethodReplacer(targetMethod.Body); replacer.RemoveSection(); replacer.Replace(method); }
public void TestMethod_RetVoid_ArgInt() { //Assign var state = 4; var targetMethod = typeof(A_RetVoid_ArgInt).GetMethod(nameof(A_RetVoid_ArgInt.Execute)); var injectedMethod = typeof(A_RetVoid_ArgInt).GetMethod(nameof(A_RetVoid_ArgInt.ExecuteWithStateChange)); var a = new A_RetVoid_ArgInt(); a.Execute(state); var oldState = a.State; //Act MethodReplacer.Replace(targetMethod, injectedMethod); a.Execute(state); //Assert var newState = a.State; Assert.AreEqual(oldState, 0); Assert.AreEqual(newState, state); }
public void DynamicMethodCreatorTestCreate() { var targetMethod = typeof(A).GetMethod(nameof(A.Execute)); var injectedMethod = DynamicMethodCreator.DynamicMethodCreator.CreateMethodSameAsOrigial(targetMethod); MethodReplacer.Replace(targetMethod, injectedMethod); new A().Execute(); }
public async Task ReplaceStaticMethodFromAnotherClass() { //Assign var state = 1; var targetMethod = typeof(A).GetMethod(nameof(A.ExecuteStatic)); var injectedMethod = typeof(B).GetMethod(nameof(B.ExecuteStatic)); var oldState = await A.ExecuteStatic(state); //Act MethodReplacer.Replace(targetMethod, injectedMethod); var newState = await A.ExecuteStatic(state); //Assert Assert.AreEqual(oldState, 100); Assert.AreEqual(newState, 200); }
public async Task ReplaceNonStaticWithStaticUnexpectedBehavior() { //Assign var state = 1; var targetMethod = typeof(A_UB).GetMethod(nameof(A_UB.ExecuteStatic)); var injectedMethod = typeof(B_UB).GetMethod(nameof(B_UB.Execute)); var oldState = await A_UB.ExecuteStatic(state); //Act MethodReplacer.Replace(targetMethod, injectedMethod); var newState = await A_UB.ExecuteStatic(state); //Assert Assert.AreEqual(oldState, 100); Assert.AreNotEqual(newState, oldState); Assert.AreNotEqual(newState, 20); }
public async Task TestMethod_RetInt_ArgInt_Async() { //Assign var state = 4; var targetMethod = typeof(A_RetInt_ArgInt_Async).GetMethod(nameof(A_RetInt_ArgInt_Async.Execute)); var injectedMethod = typeof(A_RetInt_ArgInt_Async).GetMethod(nameof(A_RetInt_ArgInt_Async.ExecuteWithStateChange)); var a = new A_RetInt_ArgInt_Async(); var oldState = await a.Execute(state); //Act MethodReplacer.Replace(targetMethod, injectedMethod); var newState = await a.Execute(state); //Assert Assert.AreEqual(oldState, 0); Assert.AreEqual(newState, state); }