Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

AOP using System.Reflection.Emit - Code Injection IL

, 2 Jun 2006
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.
interceptor.zip
WinTestAspect
App.ico
bin
Debug
SadasSof.Aspects.dll
WinTestAspect.exe
obj
Debug
temp
TempPE
WinTestAspect.csproj.user
SadasSoft.Aspects
bin
Debug
SadasSof.Aspects.dll
obj
Debug
InterceptorSample.dll
temp
TempPE
Release
Sadas.Reflection.dll
temp
TempPE
SadasSof.Aspects.csproj.user
WebTestAspects
bin
EnterpriseDemo.dll
SadasSof.Aspects.dll
WebTestAspects.dll
Global.asax
WebTestAspects.csproj.webinfo
wintestaspect.zip
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SadasSof.Aspects;
using EnterpriseDemo;
using SadasSof.Aspects.Attributes;

namespace WebTestAspects
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public  class WebForm1 : System.Web.UI.Page
				   {
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.CheckBox ckbFilter;
		protected System.Web.UI.WebControls.RadioButtonList rdDelegaton;
		protected System.Web.UI.WebControls.Label Label1;
	
					   protected void Page_Load(object sender, System.EventArgs e)
					   {
						   if(!Page.IsPostBack)
						   {
							   ListItem lI1=new ListItem(BussinesLogicEmployees.Delegation.Madrid.ToString());
							   lI1.Selected=true;
							   rdDelegaton.Items.Add(lI1);
							   rdDelegaton.Items.Add(new ListItem(BussinesLogicEmployees.Delegation.Paris.ToString()) );
							   rdDelegaton.Items.Add(new ListItem(BussinesLogicEmployees.Delegation.London.ToString()) );
						   }
					   }

					   #region Web Form Designer generated code
					   override protected void OnInit(EventArgs e)
					   {
						   //
						   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
						   //
						   InitializeComponent();
						   base.OnInit(e);
					   }
		
					   /// <summary>
					   /// Required method for Designer support - do not modify
					   /// the contents of this method with the code editor.
					   /// </summary>
					   private void InitializeComponent()
					   {    
						   this.Button1.Click += new System.EventHandler(this.Button1_Click);

					   }
					   #endregion

					   protected void Button1_Click(object sender, System.EventArgs e)
					   {
		
						   IBussinesLogicEmployees iBLExternal=null;

						   if(ckbFilter.Checked)
							   iBLExternal=(IBussinesLogicEmployees)CodeInjection.Create(
								   new BussinesLogicEmployees(),
								   typeof(IBussinesLogicEmployeesExternalFilter));
						   else
							   iBLExternal=(IBussinesLogicEmployees)CodeInjection.Create(
								   new BussinesLogicEmployees(),
								   typeof(IBussinesLogicEmployeesAll));
			
						   Employees dsE=null;
			
						   switch(rdDelegaton.SelectedValue)
						   {
							   case "Madrid":
								   dsE = iBLExternal.GetEmployees(BussinesLogicEmployees.Delegation.Madrid);
								   break;

							   case "Paris":
								   dsE = iBLExternal.GetEmployees(BussinesLogicEmployees.Delegation.Paris);
								   break;

							   case "London":
								   dsE = iBLExternal.GetEmployees(BussinesLogicEmployees.Delegation.London);
								   break;
						   }
			
						   TextBox1.Text = CountingCalls.Calls("GetEmployees").ToString();
			
						   DataGrid1.DataSource=dsE;
						   DataGrid1.DataBind();
		
					   }



					   public interface IBussinesLogicEmployees
					   {
						   EnterpriseDemo.Employees GetEmployees(EnterpriseDemo.BussinesLogicEmployees.Delegation delegation);
		
					   }
		
					   public interface IBussinesLogicEmployeesAll:IBussinesLogicEmployees
					   {
						   [CountingCalls]
						   [LoggerExceptionToFile(@"C:\Inetpub\wwwroot\WebTestAspects\logExceptions.txt")]
						   new EnterpriseDemo.Employees GetEmployees(EnterpriseDemo.BussinesLogicEmployees.Delegation delegation);
					   }

					   public interface IBussinesLogicEmployeesExternalFilter:IBussinesLogicEmployees
					   {
						   [CountingCalls]
						   [ExternalFilter]
						   [LoggerToFile(@"C:\Inetpub\wwwroot\WebTestAspects\log.txt")]
						   [LoggerExceptionToFile(@"C:\Inetpub\wwwroot\WebTestAspects\logExceptions.txt")]
						   new EnterpriseDemo.Employees GetEmployees(EnterpriseDemo.BussinesLogicEmployees.Delegation delegation);
					   }
				   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

About the Author

Roberto Loreto
Web Developer
Spain Spain
No Biography provided

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