Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept method calls in C#

I'd like to intercept and inject custom code when calling 3rd party code in C#. I am using an external library (AutoIt) for GUI automation. The AutoIt dll is provided without source code.

All actions done with this framework are performed from a single class (AutoItClass) providing access to all the methods. I'd like to be able to inject custom code when calling methods on this class, is this possible? For example:

  1. Log some information from within the called method.
  2. Perform any other action from within the method (wait for X seconds).

This would be possible very simply by inheriting from this class and overriding all its methods (which is a must since this is a COM object), but this is not the preferred way. Any comments will be helpful!

like image 446
lysergic-acid Avatar asked Mar 20 '11 08:03

lysergic-acid


People also ask

How do you write an interceptor in C#?

Creating an interceptor is very simple; just declare a class that implements the intercept () method of the HttpInterceptor Interface, which can be imported from @angular/common/http. Below is the code snippet of newly created Interceptor. Intercept method will take 2 arguments.

What is interception in C#?

Interception. Fody provides basic interceptors to intercept methods, properties and constructors and its feature-set is aimed towards eliminating boilerplate code.

Which is the preferred way to intercept Java calls?

Defining Interceptors Interceptors can be defined in Java as an method interceptor or a class interceptor. The preferred way to define in Java code is by using meta-data annotations. They can be defined in the application descriptor as well, but, in that case they are not portable across Java EE servers.


2 Answers

I wouldn't use inheritance - you can use composition here. Create your own class which has the same methods - or in fact only the ones you're interested in - and delegate through that. That way you can be sure you won't "miss" any methods accidentally, because anything you don't implement won't be callable through the rest of your codebase... so long as you make sure the rest of your code doesn't refer to the original library class, of course.

like image 197
Jon Skeet Avatar answered Oct 11 '22 21:10

Jon Skeet


You can investigate PostSharp, which is a commercial product that can inject IL into compiled assemblies to perform aspect oriented programming. You can define different kind of behaviour that should happen before and after a method gets executed, for example, which seems to be what you want. This way, as PostSharp handles this in a post-compilation step, you don't need to create any inherited classes from the classes that you want to intercept.

Otherwise if you want a more "pure" solution I would follow Jon's advice about creating a new class that wraps the functionality of the one that you want to intercept. (see Decorator pattern) .

like image 32
Can Gencer Avatar answered Oct 11 '22 19:10

Can Gencer