Click here to Skip to main content
15,891,905 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.6K   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.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Reflection.Emit;
using EnterpriseDemo;
using SadasSof.Aspects;
using SadasSof.Aspects.Attributes;

namespace WinTestAspect
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.CheckBox ckbFilter;
		private System.Windows.Forms.TextBox TextBox1;
		private System.Windows.Forms.DataGrid DataGrid1;
		private System.Windows.Forms.RadioButton rdDelegation1;
		private System.Windows.Forms.RadioButton rdDelegation2;
		private System.Windows.Forms.RadioButton rdDelegation3;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.TextBox1 = new System.Windows.Forms.TextBox();
			this.DataGrid1 = new System.Windows.Forms.DataGrid();
			this.ckbFilter = new System.Windows.Forms.CheckBox();
			this.rdDelegation1 = new System.Windows.Forms.RadioButton();
			this.rdDelegation2 = new System.Windows.Forms.RadioButton();
			this.rdDelegation3 = new System.Windows.Forms.RadioButton();
			((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(40, 56);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(80, 23);
			this.button1.TabIndex = 0;
			this.button1.Text = "GetEmployees";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// TextBox1
			// 
			this.TextBox1.Location = new System.Drawing.Point(400, 64);
			this.TextBox1.Name = "TextBox1";
			this.TextBox1.Size = new System.Drawing.Size(64, 20);
			this.TextBox1.TabIndex = 3;
			this.TextBox1.Text = "";
			// 
			// DataGrid1
			// 
			this.DataGrid1.DataMember = "";
			this.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.DataGrid1.Location = new System.Drawing.Point(32, 104);
			this.DataGrid1.Name = "DataGrid1";
			this.DataGrid1.Size = new System.Drawing.Size(440, 208);
			this.DataGrid1.TabIndex = 6;
			this.DataGrid1.Visible = false;
			// 
			// ckbFilter
			// 
			this.ckbFilter.Location = new System.Drawing.Point(128, 56);
			this.ckbFilter.Name = "ckbFilter";
			this.ckbFilter.TabIndex = 7;
			this.ckbFilter.Text = "External Filter";
			// 
			// rdDelegation1
			// 
			this.rdDelegation1.Checked = true;
			this.rdDelegation1.Location = new System.Drawing.Point(240, 16);
			this.rdDelegation1.Name = "rdDelegation1";
			this.rdDelegation1.TabIndex = 9;
			this.rdDelegation1.TabStop = true;
			this.rdDelegation1.Text = "Madrid";
			// 
			// rdDelegation2
			// 
			this.rdDelegation2.Location = new System.Drawing.Point(240, 40);
			this.rdDelegation2.Name = "rdDelegation2";
			this.rdDelegation2.TabIndex = 10;
			this.rdDelegation2.Text = "Paris";
			// 
			// rdDelegation3
			// 
			this.rdDelegation3.Location = new System.Drawing.Point(240, 64);
			this.rdDelegation3.Name = "rdDelegation3";
			this.rdDelegation3.TabIndex = 11;
			this.rdDelegation3.Text = "London";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(496, 350);
			this.Controls.Add(this.rdDelegation3);
			this.Controls.Add(this.rdDelegation2);
			this.Controls.Add(this.rdDelegation1);
			this.Controls.Add(this.ckbFilter);
			this.Controls.Add(this.DataGrid1);
			this.Controls.Add(this.TextBox1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.DataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{

		}

		private void button1_Click(object sender, System.EventArgs e)
		{

			try
			{
				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;

				string delegation=string.Empty;
				if(rdDelegation1.Checked)
					delegation="Madrid";
				else if(rdDelegation2.Checked)
					delegation="Paris";
				else
					delegation="London";
			
				switch(delegation)
				{
					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.Visible=true;
				DataGrid1.DataSource=dsE.Tables[0];
			}
			catch(Exception ex)
			{
				DataGrid1.Visible=false;
				MessageBox.Show(ex.InnerException.ToString());
			}
			
		}



	}



	public interface IBussinesLogicEmployees
	{
		EnterpriseDemo.Employees GetEmployees(EnterpriseDemo.BussinesLogicEmployees.Delegation delegation);
		
	}
		
	public interface IBussinesLogicEmployeesAll:IBussinesLogicEmployees
	{
		[CountingCalls]
		[LoggerExceptionToFile]
		new EnterpriseDemo.Employees GetEmployees(EnterpriseDemo.BussinesLogicEmployees.Delegation delegation);
	}

	public interface IBussinesLogicEmployeesExternalFilter:IBussinesLogicEmployees
	{
		[CountingCalls]
		[ExternalFilter]
		[LoggerToFile]
		[LoggerExceptionToFile]
		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