using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace VCommon.Reflection { public static class AssemblyGitHashLookup { private const string MetaKey = "GitHash"; private static readonly LazyHolder>> Holder = new LazyHolder>>(CreateDic); public static IReadOnlyDictionary> All => Holder.Instance; public static IReadOnlyDictionary> AllNames => All.ToDictionary(p => p.Key, p => (IReadOnlyCollection)p.Value.Select(a => a.GetName().Name).ToArray()); private static IReadOnlyDictionary> CreateDic() { var dic = AppDomain.CurrentDomain.GetAssemblies().Where(p => { if (false == p.IsDefined(typeof(AssemblyMetadataAttribute))) return false; return p.GetCustomAttributes().Any(attr => attr.Key == MetaKey); }).GroupBy(p => p.GetCustomAttributes() .Where(attr => attr.Key == MetaKey) .Select(attr => attr.Value) .First() ).ToDictionary(p => p.Key, p => (IReadOnlyCollection)p.ToArray()); return dic; } } }