Click here to Skip to main content
15,885,546 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.3K   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.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Threading;

namespace SadasSof.Aspects.Attributes
{
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property |
	 AttributeTargets.Interface ,Inherited=true)]
	public abstract  class AspectAttribute : Attribute
	{
		public abstract object Action(object target, MethodBase method, object[] parameters, object result);
	}

	public abstract class BeforeAttribute: AspectAttribute
	{
	}

	public abstract class AfterAttribute: AspectAttribute
	{
	}

	public abstract class LogExceptionAttribute: AspectAttribute
	{
	}


	public class LoggerExceptionToFile:LogExceptionAttribute
	{
		string pathInternal = @"c:\logException.txt";

		public LoggerExceptionToFile(string path)
		{
			pathInternal = path;
			
		}

		public LoggerExceptionToFile()
		{	
		}

		public override object Action(object target, MethodBase method, object[] parameters, object result)
		{
			string namePrincipal = Thread.CurrentPrincipal.Identity.Name;
			if(namePrincipal==string.Empty)
				namePrincipal="Anonymous User";

			namePrincipal="User: " + namePrincipal;

			string text = "Assambly: " + target.ToString() +
			"\nMethod: " + method.Name+
			"\n\nInnerException: " + ((Exception)result).InnerException;

			string content = SadasSof.Aspects.Helper.ReadFile(pathInternal);
			
			try
			{
				SadasSof.Aspects.Helper.SaveToFile(namePrincipal, text,content, pathInternal);
			}
			catch
			{
				throw;
			}

			return null;
		}
	}


	public class LoggerToFile:BeforeAttribute
	{
		string pathInternal = @"c:\log.txt";

		public LoggerToFile(string path)
		{
			pathInternal = path;
			
		}

		public LoggerToFile()
		{	
		}

		public override object Action(object target, MethodBase method, object[] parameters, object result)
		{
			string namePrincipal = Thread.CurrentPrincipal.Identity.Name;
			if(namePrincipal==string.Empty)
					namePrincipal="Anonymous User";

			namePrincipal="User: " + namePrincipal;

			string text = "Assambly: " + target.ToString() +"\nMethod: " + method.Name;

			string content = SadasSof.Aspects.Helper.ReadFile(pathInternal);
			
			try
			{
				SadasSof.Aspects.Helper.SaveToFile(namePrincipal, text,content, pathInternal);
			}
			catch
			{
				throw;
			}

			return null;
		}
	}


	public class CountingCalls:BeforeAttribute
	{
		static Hashtable calls;

		public override object Action(object target, MethodBase method, object[] parameters, object result)
		{
			if(calls==null)
				calls=new Hashtable();

			if(calls[method.Name]==null)
				calls[method.Name]=1;
			else
				calls[method.Name]=((int)calls[method.Name])+1;

			return null;

		}

		public static int Calls(string methodName)
		{
			
				if(calls==null ||calls[methodName]==null)
					return 0;
				else 
					return (int)calls[methodName];
			   
		}
	}


	public class ExternalFilter:AfterAttribute
	{

		public override object Action(object target, MethodBase method, object[] parameters, object result)
		{
		
			DataRowCollection drC= ((DataSet)result).Tables[0].Rows;
		

			for(int i=0;i<drC.Count;i++)
			{
				if(!(bool)drC[i]["External"])
				{
					drC[i].Delete();
					i--;
				}
			}
			
			 ((DataSet)result).AcceptChanges();

			return result;

		}


		

	}




}

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