Deduce Root Namespace Parts - CSharp System.Reflection

CSharp examples for System.Reflection:Assembly

Description

Deduce Root Namespace Parts

Demo Code


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

public class Main{
        public static IEnumerable<string> DeduceRootNamespaceParts(Assembly assembly)
        {/*w w  w. ja  v  a  2s.  c o  m*/
            var splitNamespaces = assembly.GetTypes()
                .Where(type => type.IsPublic)
                .Select(type => type.Namespace)
                .Distinct()
                .Where(_ => _ != "JetBrains.Annotations")
                .Select(ns => ns.Split('.'))
                .ToArray();

            var largestCommonality = 0;

            do
            {
                if (largestCommonality >= splitNamespaces[0].Length)
                    break;

                var valueToTest = splitNamespaces[0][largestCommonality];
                var commonality = largestCommonality;
                var areAllSame = splitNamespaces.All(strings => strings.Length > commonality && strings[commonality] == valueToTest);
                if (!areAllSame)
                    break;
                largestCommonality++;
            } while (true);

            return splitNamespaces[0].Take(largestCommonality);
        }
}

Related Tutorials