Hi,
Thanks for the explanation, What you trying to do is possible with
CThru.
Basically, what you have to do is:
1) Add CThru to your references.
2) Write a custom aspect class that inherits Aspect class.
3) Add the custom aspect to CThruEngine.
4) Override Should intercept in the custom aspect class
This method should return bool on every class method that you want to intercept.
5) Override MethodBehavior method in the custom aspect class
In this method you should specify the desired behavior of the intercepted method.
6) Load Typemock assembly in your main (to enable CThru).
* Intersaption of types from mscorelib.dll is not possible in the main assembly.
See the example below:
class MyAspect : Aspect
{
public override bool ShouldIntercept(InterceptInfo info)
{
// check if the getter of "Now" property should be intercepted
return info.MethodName == "get_Now";
}
public override void MethodBehavior(DuringCallbackEventArgs e)
{
base.MethodBehavior(e);
// allow the method to return custom value
e.MethodBehavior = MethodBehaviors.ReturnsCustomValue;
// set the value you want to return
e.ReturnValueOrException = new DateTime(1, 1, 1);
}
}
class Program
{
static void Main(string[] args)
{
//force loading Typemock assembly.
var assembly = typeof(MockManager).Assembly;
CThruEngine.AddAspect(new MyAspect());
CThruEngine.StartListening();
//Class1 is not in the main assembly so its call to dateTime.Now will be intercepted
Console.WriteLine(new Class1().DoDo());
}
}
namespace ClassLibrary1
{
public class Class1
{
public DateTime DoDo()
{
return DateTime.Now;
}
}
}
Please let me know if it helps.