Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#

Introducing the LinFu Framework, Part II: LinFu.DynamicObject – Adding Dynamic Language Features to Statically Typed Languages

Rate me:
Please Sign up or sign in to vote.
4.97/5 (50 votes)
12 Nov 2007LGPL316 min read 144.7K   882   67  
Using LinFu.DynamicObject to add mixins, duck typing and multiple dispatch to your favorite .NET languages
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Simple.IoC.Loaders.Interfaces;

namespace Simple.IoC.Loaders
{
    public abstract class BaseLoader
    {
        private IContainer _container;
        protected IAssemblyLoader _assemblyLoader = new AssemblyLoader();
        protected ITypeLoader _typeLoader = new TypeLoader();
        private ILoadStrategy _loadStrategy;
        protected BaseLoader()
        {
        }
            
        public BaseLoader(IContainer container)
        {
            _container = container;
        }

        public BaseLoader(IContainer container, IAssemblyLoader loader)
        {
            _container = container;
            _assemblyLoader = loader;
        }

        public BaseLoader(IContainer container, IAssemblyLoader loader, ITypeLoader typeLoader)
        {
            _container = container;
            _assemblyLoader = loader;            
            _typeLoader = typeLoader;
        }

        public virtual void LoadDirectory(string directory, string fileSpec)
        {
            string[] assemblyFiles = Directory.GetFiles(Path.GetFullPath(directory), Path.GetFileName(fileSpec));

            // Load each assembly and search for types that
            // implement IFactory<T>
            List<Type> loadedTypes = new List<Type>();
            foreach (string assemblyFile in assemblyFiles)
            {
                Assembly currentAssembly = AssemblyLoader.LoadAssembly(assemblyFile);
                if (currentAssembly == null)
                    continue;

                Type[] currentTypes = TypeLoader.LoadTypes(currentAssembly);
                if (currentTypes == null || currentTypes.Length == 0)
                    continue;

                loadedTypes.AddRange(currentTypes);
            }
            
            if (_loadStrategy == null)
                return;
            
            _loadStrategy.ProcessLoadedTypes(_container, loadedTypes);
        }        

        public IAssemblyLoader AssemblyLoader
        {
            get { return _assemblyLoader; }
            set { _assemblyLoader = value; }
        }

        public ITypeLoader TypeLoader
        {
            get { return _typeLoader; }
            set { _typeLoader = value; }
        }

        public ILoadStrategy LoadStrategy
        {
            get { return _loadStrategy; }
            set { _loadStrategy = value; }
        }

        public IContainer Container
        {
            get { return _container; }
            set { _container = value; }
        }
    }
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Readify
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions