Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#

Applied Use of LinFu/Cecil and Aspect-Oriented Programming Concepts - A Library

Rate me:
Please Sign up or sign in to vote.
4.88/5 (8 votes)
4 Sep 2008CPOL17 min read 35.2K   160   32  
A library of useful functionality using Aspect-Oriented Programming concepts, and implemented using the LinFu and Cecil.Mono projects/frameworks.
using System;
using System.Reflection;
using System.Windows.Forms;

using System.Collections.Generic;
using System.IO;
using System.ComponentModel;
using System.Collections;
using System.Text;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;

using BrainTechLLC.ThreadSafeObjects;
using LinFu.AOP;
using LinFu.AOP.Interfaces;

namespace BrainTechLLC
{
	[Serializable, AttributeUsage(AttributeTargets.Method)]
	public class InvokeIfThenElseAttribute : IfCaseAttribute, IReadableAttribute
	{
		public InvokeIfThenElseAttribute(string ifMethod, string thenMethod, string elseMethod,
			string ifMethodAfter, string thenMethodAfter, string elseMethodAfter)
			: base(ifMethod, thenMethod, elseMethod, ifMethodAfter, thenMethodAfter, elseMethodAfter) { }
	}

	[Serializable, AttributeUsage(AttributeTargets.Method)]
	public class IfCaseAttribute : Attribute, IReadableAttribute
	{
		public string IfMethodName { get; set; }
		public string ThenMethodName { get; set; }
		public string ElseMethodName { get; set; }
		public string IfMethodNameAfter { get; set; }
		public string ThenMethodNameAfter { get; set; }
		public string ElseMethodNameAfter { get; set; }

		public object Value { get { return IfMethodName; } }

		public IfCaseAttribute(string ifMethod, string thenMethod, string elseMethod,
			string ifMethodAfter, string thenMethodAfter, string elseMethodAfter)
		{
			IfMethodName = ifMethod;
			ThenMethodName = thenMethod;
			ElseMethodName = elseMethod;
			IfMethodNameAfter = ifMethodAfter;
			ThenMethodNameAfter = thenMethodAfter;
			ElseMethodNameAfter = elseMethodAfter;
		}

		protected void GetIfThenElseMethodsBefore(Type t, out MethodInfo ifDel, out MethodInfo thenDel, out MethodInfo elseDel)
		{
			ifDel = Lookups.IfLookup[t.Name + "." + IfMethodName];
			thenDel = null;
			elseDel = null;

			if (ifDel == null)
			{
				ifDel = t.GetMethod(IfMethodName);
				Lookups.IfLookup.AddOrSet(t.Name + "." + IfMethodName, ifDel);
			}

			if (!string.IsNullOrEmpty(ThenMethodName))
			{
				thenDel = Lookups.ThenLookup[t.Name + "." + ThenMethodName];

				if (thenDel == null)
				{
					thenDel = t.GetMethod(ThenMethodName);
					Lookups.ThenLookup.AddOrSet(t.Name + "." + ThenMethodName, thenDel);
				}
			}

			if (!string.IsNullOrEmpty(ElseMethodName))
			{
				elseDel = Lookups.ElseLookup[t.Name + "." + ElseMethodName];

				if (elseDel == null)
				{
					elseDel = t.GetMethod(ElseMethodName);
					Lookups.ElseLookup.AddOrSet(t.Name + "." + ElseMethodName, elseDel);
				}
			}
		}

		protected void GetIfThenElseMethodsAfter(Type t, out MethodInfo ifDel, out MethodInfo thenDel, out MethodInfo elseDel)
		{
			ifDel = Lookups.IfLookup[t.Name + "." + IfMethodNameAfter];
			thenDel = null;
			elseDel = null;

			if (!string.IsNullOrEmpty(ThenMethodNameAfter))
			{
				thenDel = Lookups.ThenLookup[t.Name + "." + ThenMethodNameAfter];

				if (thenDel == null)
				{
					thenDel = t.GetMethod(ThenMethodNameAfter);

					if (thenDel != null)
						Lookups.ThenLookup.AddOrSet(t.Name + "." + ThenMethodNameAfter, thenDel);
				}
			}

			if (!string.IsNullOrEmpty(ElseMethodNameAfter))
			{
				elseDel = Lookups.ElseLookup[t.Name + "." + ElseMethodNameAfter];

				if (elseDel == null)
				{
					elseDel = t.GetMethod(ElseMethodNameAfter);

					if (elseDel != null)
						Lookups.ElseLookup.AddOrSet(t.Name + "." + ElseMethodNameAfter, elseDel);
				}
			}

			if (ifDel == null)
			{
				ifDel = t.GetMethod(IfMethodNameAfter);

				if (ifDel != null)
					Lookups.IfLookup.AddOrSet(t.Name + "." + IfMethodNameAfter, ifDel);
			}
		}

		public void DoWorkBefore(IInvocationContext context, object thisObject)
		{
			Type t = thisObject.GetType();
			MethodInfo ifDel;
			MethodInfo thenDel;
			MethodInfo elseDel;
			GetIfThenElseMethodsBefore(t, out ifDel, out thenDel, out elseDel);

			if (ifDel == null)
			{
				if (thenDel != null)
					thenDel.Invoke(thisObject, new object[] { context });
			}
			else
			{
				object result = ifDel.Invoke(thisObject, new object[] { context });

				if (Convert.ToBoolean(result) == true)
				{
					if (thenDel != null)
						thenDel.Invoke(thisObject, new object[] { context });
				}
				else
				{
					if (elseDel != null)
						elseDel.Invoke(thisObject, new object[] { context });
				}
			}
		}

		public void DoWorkAfter(IInvocationContext context, object thisObject)
		{
			Type t = thisObject.GetType();
			MethodInfo ifDel;
			MethodInfo thenDel;
			MethodInfo elseDel;
			GetIfThenElseMethodsAfter(t, out ifDel, out thenDel, out elseDel);
			
			if (ifDel == null)
			{
				if (thenDel != null)
					thenDel.Invoke(thisObject, new object[] { context });
			}
			else
			{
				object result = ifDel.Invoke(thisObject, new object[] { context });

				if (Convert.ToBoolean(result) == true)
				{
					if (thenDel != null)
						thenDel.Invoke(thisObject, new object[] { context });
				}
				else
				{
					if (elseDel != null)
						elseDel.Invoke(thisObject, new object[] { context });
				}
			}
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Troppus Software
United States United States
Currently working as a Senior Silverlight Developer with Troppus Software in Superior, CO. I enjoy statistics, programming, new technology, playing the cello, and reading codeproject articles. Smile | :)

Comments and Discussions