CSharpGL.AssemblyHelper.GetAllDerivedTypes C# (CSharp) Method

GetAllDerivedTypes() public static method

Get all derived non-abstract types of specified base type from all loaded assemblies.
public static GetAllDerivedTypes ( this baseType, bool>.Func addtionalFilter = null ) : List
baseType this
addtionalFilter bool>.Func addtional filter.
return List
        public static List<Type> GetAllDerivedTypes(this Type baseType, Func<Type, bool> addtionalFilter = null)
        {
            //if (addtionalFilter == null) { addtionalFilter = x => !x.IsAbstract; }
            var result = new List<Type>();
            Assembly[] assemblies = AssemblyHelper.GetAssemblies(Application.ExecutablePath);
            foreach (Assembly asm in assemblies)
            {
                try
                {
                    var list = from item in asm.GetTypes()
                               where baseType.IsAssignableFrom(item)
                               && (addtionalFilter == null || (addtionalFilter(item)))
                               orderby item.FullName
                               select item;
                    foreach (Type item in list.Distinct())
                    {
                        result.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Assembly Helper: {0}", ex));
                }
            }

            return result;
        }

Usage Example

Example #1
0
        private object GetVariable <T>(T value, string varNameInShader)
        {
            Type t = value.GetType();
            Type varType;

            if (variableDict == null)
            {
                variableDict = new Dictionary <Type, Type>();
                var types = AssemblyHelper.GetAllDerivedTypes(
                    typeof(UniformSingleVariableBase), x => !x.IsAbstract);
                foreach (var item in types)
                {
                    try
                    {
                        // example: variableDict.Add(typeof(int), typeof(UniformInt32));
                        variableDict.Add(item.GetProperty("Value").PropertyType, item);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            if (variableDict.TryGetValue(t, out varType))
            {
                return(Activator.CreateInstance(varType, varNameInShader));
            }
            else
            {
                throw new Exception(string.Format(
                                        "UniformVariable type [{0}] doesn't exists or not included in the variableDict!",
                                        t));
            }
        }
All Usage Examples Of CSharpGL.AssemblyHelper::GetAllDerivedTypes