using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace DXGfxLib
{
    public class ReflectionHelper
    {
        public static Hashtable ListCreatableClasses()
        {
            return ListCreatableClasses(typeof(ReflectionHelper).Assembly);
        }

        public static Hashtable ListCreatableClasses(Assembly fromAssembly)
        {
            Hashtable hashTable = new Hashtable();

            Type[] types = fromAssembly.GetTypes();

            foreach (Type t in types)
            {
                if (t.IsClass && t.IsPublic)
                {
                    hashTable.Add(t.FullName, t);
                }
            }

            return hashTable;
        }

        public static Object GetInstanceOfType(Type theType)
        {
            ConstructorInfo ctor = theType.GetConstructor(new Type[] { });
            Object[] args = new Object[] { };
            Object obj = ctor.Invoke(args);
            return obj;
        }

        public static Object GetInstanceOfType(string typeName)
        {
            return GetInstanceOfType(typeof(ReflectionHelper).Assembly, typeName);
        }

        public static Object GetInstanceOfType(Assembly sourceAssembly, string typeName)
        {
            Type theType = sourceAssembly.GetType(typeName);

            if (theType == null)
                return null;

            return GetInstanceOfType(theType);
        }
    }
}
