Click here to Skip to main content
15,893,668 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.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;

namespace Simple.IoC.Loaders
{
    public abstract class BaseFactoryLoader : IFactoryLoader 
    {
        #region IFactoryLoader Members

        public virtual void LoadFactory(IContainer container, Type loadedType)
        {
            LoadAdditionalFactories(container, loadedType);

            if (!CanLoad(loadedType))
                return;

            IEnumerable<Type> itemTypes = GetItemTypes(loadedType);

			foreach(Type itemType in itemTypes)
			{
	            Debug.Assert(itemType != null);
	            if (itemType == null)
	                continue;

	            Type factoryType = GetFactoryType(itemType);

	            // Create the factory itself
	            object instance = CreateFactory(factoryType, loadedType, itemType);
	            object factoryInstance = Cast(factoryType, instance);
			    
	            if (factoryInstance == null)
	                continue;

                InsertFactory(container, itemType, loadedType, factoryInstance);
            }
        }
        protected virtual void LoadAdditionalFactories(IContainer container, Type loadedType)
        {
        }
        protected virtual void InsertFactory(IContainer container, Type itemType, Type loadedType, object factoryInstance)
        {
            // Add the object factory to the container
            MethodInfo addFactoryDefinition =
                typeof(BaseFactoryLoader).GetMethod("AddFactory", BindingFlags.NonPublic | BindingFlags.Static);

            Debug.Assert(addFactoryDefinition.IsGenericMethodDefinition);

            MethodInfo addFactory = addFactoryDefinition.MakeGenericMethod(itemType);
            addFactory.Invoke(null, new object[] { factoryInstance, container });
        }

        #endregion

        protected abstract object CreateFactory(Type factoryType, Type currentType, Type serviceType);
        protected abstract bool CanLoad(Type loadedType);
        protected virtual Type GetFactoryType(Type currentType)
        {
            return typeof (IFactory<>).MakeGenericType(currentType);
        }
        protected abstract IEnumerable<Type> GetItemTypes(Type currentType);

        #region Private Members
        private static void AddFactory<T>(object factoryInstance, IContainer container)
        {
            IFactory<T> factory = factoryInstance as IFactory<T>;
            container.AddFactory(factory);
        }
        private static object Cast(Type targetType, object instance)
        {
            MethodInfo castMethodDefinition = typeof(BaseFactoryLoader).GetMethod("CastAs", BindingFlags.NonPublic | BindingFlags.Static);
            Debug.Assert(castMethodDefinition != null);

            MethodInfo castMethod = castMethodDefinition.MakeGenericMethod(targetType);
            return castMethod.Invoke(null, new object[] { instance });
        }
        private static object CastAs<T>(object instance)
            where T : class
        {
            return instance as T;
        }
        #endregion
    }
}

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