Click here to Skip to main content
15,888,579 members
Articles / Web Development / ASP.NET

AOP using System.Reflection.Emit - Code Injection IL

Rate me:
Please Sign up or sign in to vote.
4.74/5 (22 votes)
2 Jun 20063 min read 95.4K   1.8K   75  
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.
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


Written By
Web Developer
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions