How to use the CThru engine in your own code
There are two interfaces for CThru:
- CThruEngine class : responsinble for sending events and notifications.
- Inheriting from Aspect in your code and notifying CThruEngine about your aspects.
Step 1 - Identify what you want to accomplish.
In our example, we will want to accomplish a simple thing. We want to log
every call to a method in a specific namespace.
Step 2 - Create aspect(s) that performs the actions you need
- Create a class that inherits from Aspect. let's call it LoggingAspect.
- The class can override 3 possible methods: ConstructorBehavior, StaticConstructorBehavior and
MethodBehavior. In our case we want to override MethodBehavior.
The overriden method gets an Arguments parameter that contains, among other things, the name of the method that is being called, and the typename (including the full namespace) it is called on.
We will simply write out to the console the method name and type name if the typename starts with the namespace "System"
Step 3 - Load Aspects and initialize the CThru Engine
now we'd like to add our aspect to the CThru Engine and start receiving calls in our aspect. To do this we call CThru.AddAspectsInAssembly(Assembly.GetExecutingAssembly())
- now we can start receieving the events by calling CThruEngine.StartListening(). Make any call from that moment on, and your aspect will be notified.
- To disable you can call CThruEngine.StopListening().
- If you'd like to reset any aspects you can call StopListeningAndReset(). your aspect will no longer be notified even if CThruEngine is listening to events.