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
EnterpriseDemo.dll
SadasSof.Aspects.dll
WinTestAspect.exe
using System;
using System.Reflection;
using System.IO;
using SadasSof.Aspects.Attributes;

namespace SadasSof.Aspects
{

	public  class Helper
	{
		public static Type[] GetParameterTypes( MethodInfo method )
		{
			if(method==null)
				return null;
			ParameterInfo[] pIColl = method.GetParameters();

			Type[] t = new Type[pIColl.Length];


			int i=0;
			foreach(ParameterInfo pI in pIColl)
			{
				t[i] = pI.ParameterType;
				i++;
			}

			return t;
		}


        public static MethodInfo GetMethodFromType(Type type, MethodBase methodBase) 
		{           
			MethodInfo method = type.GetMethod(methodBase.Name);            
			
			return method;            
		}


		public static AspectAttribute[] AspectUnion (object[] obj)
		{

			AspectAttribute[] aAC=new AspectAttribute[obj.Length];

			int i=0;
			foreach(AspectAttribute aA in obj)
			{
				aAC[i]=aA;
				i++;
			}
			return aAC;
		
		}

		public static  void SaveToFile(string name, string message, string adit, string path)
		{
			
			try
			{


				FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            
	
				StreamWriter sw = new StreamWriter(file);
				
				
				sw.Write(adit);
				sw.Write("\n\n-------------------------------------\n\n");
				sw.Write(name + " - "+ System.DateTime.Now.ToLocalTime());
				sw.Write(sw.NewLine);
				sw.Write(message);
				sw.Write(sw.NewLine);
				sw.Close();


				// Close file
				file.Close();
	
			}
			catch(Exception ex)
			{
				string s= ex.Message;
				throw;
			}


		}

		public static string ReadFile(string path)
		{
			string s= string.Empty;
			FileStream file=null;
			StreamReader sr=null;
			try
			{
				file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);

				sr = new StreamReader(file);


				s = sr.ReadToEnd();

				sr.Close();

				file.Close();
			}
			catch
			{
			}

			return s;

		}

		
	}
}

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

Share

About the Author

Roberto Loreto
Web Developer
Spain Spain
No Biography provided

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