Esempio n. 1
0
        internal static SearcherDatabase Populate <T>(Func <Type, bool> filter = null, Func <Type, string> nameResolver = null, Func <Type, string> categoryResolver = null)
        {
            var list = new List <SearcherItem>();
            var dict = new Dictionary <string, SearcherItem>();

            var types = TypeCache.GetTypesDerivedFrom <T>();

            foreach (var type in types)
            {
                if (type.IsGenericType || type.IsAbstract || type.ContainsGenericParameters || type.IsInterface)
                {
                    continue;
                }

                if (!TypeConstruction.CanBeConstructed(type))
                {
                    continue;
                }

                if (filter != null && !filter(type))
                {
                    continue;
                }

                try
                {
                    var typeItem = new TypeSearcherItem(type, nameResolver != null ? nameResolver(type) : string.Empty);
                    var category = categoryResolver != null?categoryResolver(type) : type.Namespace ?? "Global";

                    if (!string.IsNullOrEmpty(category))
                    {
                        if (!dict.TryGetValue(category, out var item))
                        {
                            dict[category] = item = new SearcherItem(category);
                            list.Add(item);
                        }
                        item.AddChild(typeItem);
                    }
                    else
                    {
                        list.Add(typeItem);
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            foreach (var kvp in dict)
            {
                kvp.Value.Children.Sort(CompareByName);
            }

            list.Sort(CompareByName);

            return(new SearcherDatabase(list));
        }
Esempio n. 2
0
        static SearcherDatabase Populate <T>(HashSet <Type> excludes, Func <Type, string> displayNameResolver)
        {
            var list = new List <SearcherItem>();
            var dict = new Dictionary <string, SearcherItem>();

            var collection = TypeCache.GetTypesDerivedFrom <T>();

            foreach (var type in collection)
            {
                if (type.IsGenericType || type.IsAbstract || type.ContainsGenericParameters || type.IsInterface)
                {
                    continue;
                }

                if (!TypeConstruction.HasParameterLessConstructor(type))
                {
                    continue;
                }

                try
                {
                    if (excludes.Contains(type))
                    {
                        continue;
                    }

                    var category = string.Empty;
                    TypeSearcherItem typeItem = null;

                    // Fully type-based
                    if (null == displayNameResolver)
                    {
                        typeItem = new TypeSearcherItem(type);
                        category = type.Namespace ?? "Global";
                    }
                    // We control the naming
                    else
                    {
                        var displayName = displayNameResolver.Invoke(type);
                        var prefixIndex = displayName.IndexOf("/", StringComparison.InvariantCultureIgnoreCase);
                        category = prefixIndex >= 0 ? displayName.Substring(0, prefixIndex) : "Other";
                        var name = displayName.Substring(prefixIndex >= 0 ? prefixIndex + 1 : 0);
                        typeItem = new TypeSearcherItem(type, name);
                    }

                    if (!dict.TryGetValue(category, out var item))
                    {
                        dict[category] = item = new SearcherItem(category);
                        list.Add(item);
                    }

                    item.AddChild(typeItem);
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            foreach (var kvp in dict)
            {
                kvp.Value.Children.Sort(CompareByName);
            }

            list.Sort(CompareByName);

            return(new SearcherDatabase(list));
        }