namespace FantasyNetTest;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

public static class NamespaceHelper
{
    public static IEnumerable<string> GetAllNamespaces(Assembly assembly)
    {
        return assembly.GetTypes()
            .Select(t => t.Namespace)
            .Where(n => n != null)
            .Distinct();
    }

    public static IEnumerable<string> GetAllReferencedNamespaces()
    {
        var namespaces = new HashSet<string>();
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        
        foreach (var assembly in assemblies)
        {
            try
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (!string.IsNullOrEmpty(type.Namespace))
                    {
                        namespaces.Add(type.Namespace);
                    }
                }
            }
            catch (ReflectionTypeLoadException)
            {
                // 忽略无法加载类型的程序集
                continue;
            }
        }
        
        return namespaces;
    }
}