Click here to Skip to main content
15,886,032 members
Articles / Desktop Programming / WPF

The Time Machine

Rate me:
Please Sign up or sign in to vote.
4.91/5 (13 votes)
9 May 2012CPOL6 min read 34K   1.2K   32  
Long time strategy of software design and development
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace AssemblyService
{
    /// <summary>
    /// Works with asseblies
    /// </summary>
    public static class StaticExtensionAssemblyService
    {


        /// <summary>
        /// Gets first interface object
        /// </summary>
        /// <typeparam name="T">Type of interface</typeparam>
        /// <returns>First object</returns>
        public static T GetFirstInterfaceObjectFromBaseDirectory<T>() where T : class
        {
            return AppDomain.CurrentDomain.BaseDirectory.GetFirstInterfaceObject<T>();
        }
 


        /// <summary>
        /// Gets interfaces from base directory
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <returns>Interfaces</returns>
        public static IEnumerable<T> GetInterfacesFromBaseDirectory<T>() where T : class
        {
            return AppDomain.CurrentDomain.BaseDirectory.GetInterfaces<T>();
        }


        /// <summary>
        /// Gets first interface object
        /// </summary>
        /// <typeparam name="T">Type of interface</typeparam>
        /// <param name="directory">Directory</param>
        /// <returns>First object</returns>
        public static T GetFirstInterfaceObject<T>(this string directory) where T : class
        {
            IEnumerable<T> en = directory.GetInterfaces<T>();
            foreach (T t in en)
            {
                return t;
            }
            return null;
        }

        /// <summary>
        /// Gets interfaces
        /// </summary>
        /// <typeparam name="T">Interface type</typeparam>
        /// <param name="directory">Directory</param>
        /// <returns>Interface objects</returns>
        public static IEnumerable<T> GetInterfaces<T>(this string directory) where T : class
        {
            string[] fn = Directory.GetFiles(directory, "*.dll");   // Dll files
            Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies(); // Current domain assemblies
            List<string> l = new List<string>();
            foreach (Assembly a in ass) // Looking for objects in loaded assemblies
            {
                l.Add(a.Location);
                IEnumerable<T> en = a.GetInterfaces<T>();
                foreach (T t in en)
                {
                    yield return t;
                }

            }
            foreach (string f in fn) // Looking for objects in directory
            {
                if (!l.Contains(f))
                {
                   IEnumerable<T> en = Assembly.LoadFile(f).GetInterfaces<T>();
                   foreach (T t in en)
                   {
                       yield return t;
                   }
                }
            }
        }

        /// <summary>
        /// Gets interfaces from assembly (Serching of driver assembly)
        /// </summary>
        /// <typeparam name="T">Interface type</typeparam>
        /// <param name="assembly">Assembly</param>
        /// <returns>Interface objects</returns>
        public static IEnumerable<T> GetInterfaces<T>(this Assembly assembly) where T : class
        {
            Type[] types = assembly.GetTypes(); // Assembly types
            string st = typeof(T).FullName;     // Interface full name
            foreach (Type t in types)
            {
                Type ti = t.GetInterface(st);   // Gets interface
                if (ti == null)
                {
                    continue;
                }
                FieldInfo fi = t.GetField("Singleton"); // Gets Singleton field
                if (fi != null)
                {
                    yield return fi.GetValue(null) as T; // yeild Singleton
                }
            }
        }
    }
}

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
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions