Click here to Skip to main content
Click here to Skip to main content

AOP using System.Reflection.Emit - Code Injection IL

, 2 Jun 2006
Rate this:
Please Sign up or sign in to vote.
Very often, we need to intercept method calls and enable some code to run before/after a method call of an external type (.NET object). Learn how to do this.

Introduction

Many times, we need to intercept method calls, and enable some code to run before/after a method call of an external type (.NET object). This article shows how to intercept the methods of an external type, generating method proxies by using injection of IL code.

The dynamic type factory

This factory will generate the dynamic types and return them to the caller. This emulates the external type, and intercepts the specified methods added in the interface. The factory needs the external type and an interface, which establishes the methods that will be intercepted. The sample code here below creates the new type:

public static object Create(object externalTarget, Type interfaceType)
{
    Type proxyType= EmiProxyType(target.GetType(),interfaceType);

    return Activator.CreateInstance(proxyType , 
       new object[]{externalTarget,interfaceType});     
}

After providing the factory with the target object (externalTarget) and the interface (interfaceType which specifies the methods to intercept), we call the method EmiProxyType that builds the method proxy at runtime. Now, the factory can return the enhanced object through reflection.

Defining the interface

As previously said, the interface we pass to the factory specifies which methods we want to intercept; moreover, implemented attributes (AOP) will define the desired behavior.

public interface IBussinesLogicEmployees
{
    [CountingCalls]
     [LoggerToFile]
         EnterpriseDemo.Employees GetEmployees(
             EnterpriseDemo.BussinesLogicEmployees.Delegation delegation);
}

This interface sets the method GetEmployees as interceptable, and defines the attributes CountingCalls and LoggerToFile. If we do not decorate the interface method, the interception code will be empty, so the external method won't execute before/after the additional code.

Defining the attributes

The attributes have tree behaviors inside the method that create the factory:

  1. Before calling to the external method, it must inherit the "BeforeAttribute" class.
  2. After calling to the external method, it must inherit the AfterAttribute class.
  3. If the external method type throws an exception, it must inherit the LogExceptionAttribute class.

By default, implement three attributes:

  1. CountingCalls: this attribute tells the factory to count the number of calls. The action is made before the call to the method of the external type.
  2. LoggerToFile: this attribute tells the factory to generate a log file in which information relative to the access will be stored. The action is made before the call to the external method.
  3. LoggerExceptionToFile: this attribute tells the factory to generate a log file in which information relative to the exception will be stored (in case of the exception being raised by the external method).

Additionally, I have created a custom attribute, ExternalFilter, that will be performed after the execution of the method. It filters the obtained typed dataset according to whether the employee is external or not. The method generated by the factory returns a typed dataset with the external employees, if we decorate the method of the interface with this attribute. In summary, we can extend as many attributes as we want, depending on our needs.

How to intercept an external type method?

To intercept an external type method, we must follow these steps:

  1. Define an interface indicating which are the external type methods to intercept, and what actions the interception code does, depending on the attributes.
  2. Call the factory the following way:
    IBussinesLogicEmployees iBLExternal = 
        (IBussinesLogicEmployees) CodeInjection.Create(
                new BussinesLogicEmployees(), 
                typeof(IBussinesLogicEmployeesExternalFilter)
                );
  3. Execute the intercepted method:
    EnterpriseDemo.Employees dsEd =
        iBLExternal.GetEmployees(BussinesLogicEmployees.Delegation.Madrid);

In our case, looking at the interface method GetEmployees, notice that it's marked with two attributes, CountingCalls and LoggerToFile, so the factory generated method will register the number of calls and will create a log file with information about the method calls.

If, instead, we decorate our interface this way:

public interface IBussinesLogicEmployees {

[LoggerToFile]
[LoggerExceptionToFile]
[ExternalFilter]  
EnterpriseDemo.Employees GetEmployees(
    EnterpriseDemo.BussinesLogicEmployees.Delegation delegation);
}

the factory generated method will create a log file with information about the method call, will create a log file with information about the exception generated by the external type method, will execute the external type method, and finally, will filter the typed dataset returned depending on whether the employees are marked as external or not.

Conclusion

In the .NET platform, Aspect Oriented Programming (AOP) can be achieved mainly on two approaches: injecting the IL with the desired code, or alternatively, inheriting the classes from the .NET CLR Library class ContextBoundObject. Injecting the IL code allows interception of methods on external types. So, we can intercept external methods without needing to have the source code of that external type. We use interfaces to help us decide what methods will be intercepted, and decorate interface methods with attributes to run extra actions.

History

  • Release 1.0, 3 June 2006.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Roberto Loreto
Web Developer
Spain Spain
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmvpKanasz Robert28-Sep-12 8:14 
QuestionRoberto: you Rock!!!! PinmemberRicardoCasquete3-Nov-11 14:26 
GeneralMOre on .NET AOP and code injection PinmemberOscar Vera24-Mar-08 4:29 
QuestionCan I add feature to assembly using CodeDOM ? Pinmembervgrigor14-Dec-06 2:58 
AnswerRe: Can I add feature to assembly using CodeDOM ? PinmemberRoberto Loreto4-Dec-06 3:18 
GeneralRe: Can I add feature to assembly using CodeDOM ? Pinmembervgrigor14-Dec-06 10:27 
GeneralRe: Can I add feature to assembly using CodeDOM ? PinmemberRoberto Loreto4-Dec-06 12:23 
GeneralRe: Can I add feature to assembly using CodeDOM ? PinmemberBamYazi10-Jan-07 1:48 
GeneralCooooooool !!!! PinmemberRicardo Casquete5-Jun-06 22:52 
QuestionCastle? PinmemberTutu2-Jun-06 21:32 
GeneralRe: Castle? PinmemberChristophe Bertrand22-Oct-13 23:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 2 Jun 2006
Article Copyright 2006 by Roberto Loreto
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid