Exemplo n.º 1
0
        /// <summary>
        /// Collects the types that are discovered around <c>hostType</c>, according to rules defined with <c>flags</c>
        /// </summary>
        /// <param name="flags">The flags.</param>
        /// <param name="hostAssembly">The host assembly.</param>
        /// <param name="hostType">Type of the host.</param>
        /// <returns>List of types that are discovered around <c>hostType</c>, according to rules defined with <c>flags</c></returns>
        /// <seealso cref="CollectTypeFlags"/>
        public static List <Type> CollectTypes(CollectTypeFlags flags, Assembly hostAssembly = null, Type hostType = null)
        {
            List <Type> output          = new List <Type>();
            String      namespaceString = "";

            if (hostType != null)
            {
                namespaceString = hostType.Namespace;

                if (flags.HasFlag(CollectTypeFlags.ofTypeProperties))
                {
                    output.AddRange(CollectTypesOfProperties(new List <Type>()
                    {
                        hostType
                    }));
                }
            }

            if (!flags.HasFlag(CollectTypeFlags.ofThisAssembly) || flags.HasFlag(CollectTypeFlags.ofAllAssemblies))
            {
                flags |= CollectTypeFlags.ofThisAssembly;
            }

            if (flags.HasFlag(CollectTypeFlags.ofThisAssembly))
            {
                if (hostAssembly == null)
                {
                    if (hostType != null)
                    {
                        hostAssembly = hostType.Assembly;
                    }
                }

                if (hostAssembly == null)
                {
                    hostAssembly = Assembly.GetExecutingAssembly();
                }

                output.AddRange(hostAssembly.GetTypes());
            }
            else if (flags.HasFlag(CollectTypeFlags.ofAllAssemblies))
            {
                foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
                {
                    if (flags.HasFlag(CollectTypeFlags.includeNonImbAssemblies) || ass.FullName.StartsWith("imb"))
                    {
                        output.AddRange(ass.GetTypes());
                    }
                }
            }

            output = FilterTypeList(output, flags);
            if (!namespaceString.isNullOrEmpty())
            {
                output = FilterByNamespace(output, namespaceString, flags);
            }

            return(output);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Collects the types around the <c>hostType</c> - according to specified flags
        /// </summary>
        /// <param name="hostType">Type of the host.</param>
        /// <param name="flags">The flags.</param>
        /// <returns>Dictionary of collected types, keys are <see cref="Type.Name"/>s.</returns>
        public static Dictionary <String, Type> CollectTypes(this Type hostType, CollectTypeFlags flags)
        {
            Dictionary <String, Type> output = new Dictionary <string, Type>();

            List <Type> list = CollectTypes(flags, hostType.Assembly, hostType);

            foreach (Type pet in list)
            {
                if (!output.ContainsKey(pet.Name))
                {
                    output.Add(pet.Name, pet);
                }
            }

            return(output);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Filters the type list.
        /// </summary>
        /// <param name="types">The types.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        private static List <Type> FilterTypeList(List <Type> types, CollectTypeFlags flags)
        {
            List <Type> output = new List <Type>();

            foreach (Type t in types)
            {
                if (flags.HasFlag(CollectTypeFlags.includeClassTypes))
                {
                    if (t.IsClass)
                    {
                        output.Add(t);
                    }
                }
                if (flags.HasFlag(CollectTypeFlags.includeEnumTypes))
                {
                    if (t.IsEnum)
                    {
                        output.Add(t);
                    }
                }
                if (flags.HasFlag(CollectTypeFlags.includeValueTypes))
                {
                    if (t.IsValueType)
                    {
                        output.Add(t);
                    }
                }
                if (flags.HasFlag(CollectTypeFlags.includeGenericTypes))
                {
                    if (t.IsGenericType)
                    {
                        output.Add(t);
                    }
                }
            }

            if (!output.Any())
            {
                output = types;
            }

            return(output);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Filters the by namespace.
        /// </summary>
        /// <param name="types">The types.</param>
        /// <param name="namespaceString">The namespace string.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        private static List <Type> FilterByNamespace(List <Type> types, String namespaceString, CollectTypeFlags flags)
        {
            List <Type> output = new List <Type>();

            String namespaceParent = namespaceString.getPathVersion(-1);

            foreach (Type t in types)
            {
                if (!t.Name.StartsWith("<"))
                {
                    if (t.Namespace != null)
                    {
                        if (flags.HasFlag(CollectTypeFlags.ofSameNamespace))
                        {
                            if (t.Namespace == namespaceString)
                            {
                                output.Add(t);
                            }
                        }
                        if (flags.HasFlag(CollectTypeFlags.ofChildNamespaces))
                        {
                            if (t.Namespace.StartsWith(namespaceString))
                            {
                                output.Add(t);
                            }
                        }
                        if (flags.HasFlag(CollectTypeFlags.ofParentNamespace))
                        {
                            if (t.Namespace.StartsWith(namespaceParent))
                            {
                                output.Add(t);
                            }
                        }
                    }
                }
            }

            if (!output.Any())
            {
                output = types;
            }

            return(output);
        }