Пример #1
0
        //public static bool IsWithinGac(string assemblyLocation)
        //{
        //    return Core.FileUtility.IsBaseDirectory(GacRootPathV2, assemblyLocation)
        //        || Core.FileUtility.IsBaseDirectory(GacRootPathV4, assemblyLocation);
        //}

        public static List <AssemblyName> GetAssemblyList()
        {
            IApplicationContext applicationContext = null;
            IAssemblyEnum       assemblyEnum       = null;
            IAssemblyName       assemblyName       = null;

            List <AssemblyName> l = new List <AssemblyName>();

            Fusion.CreateAssemblyEnum(out assemblyEnum, null, null, 2, 0);
            while (assemblyEnum.GetNextAssembly(out applicationContext, out assemblyName, 0) == 0)
            {
                uint nChars = 0;
                assemblyName.GetDisplayName(null, ref nChars, 0);

                StringBuilder sb = new StringBuilder((int)nChars);
                assemblyName.GetDisplayName(sb, ref nChars, 0);

                l.Add(new AssemblyName(sb.ToString()));
            }
            return(l);
        }
Пример #2
0
        public static AssemblyName FindBestMatchingAssemblyName(AssemblyName name)
        {
            string[] info;
            Version  requiredVersion = name.Version;
            string   publicKey       = PublicKeyTokenToString(name);

            IApplicationContext applicationContext = null;
            IAssemblyEnum       assemblyEnum       = null;
            IAssemblyName       assemblyName;

            Fusion.CreateAssemblyNameObject(out assemblyName, name.Name, 0, 0);
            Fusion.CreateAssemblyEnum(out assemblyEnum, null, assemblyName, 2, 0);
            List <string> names = new List <string>();

            while (assemblyEnum.GetNextAssembly(out applicationContext, out assemblyName, 0) == 0)
            {
                uint nChars = 0;
                assemblyName.GetDisplayName(null, ref nChars, 0);

                StringBuilder sb = new StringBuilder((int)nChars);
                assemblyName.GetDisplayName(sb, ref nChars, 0);

                string fullName = sb.ToString();
                if (publicKey != null)
                {
                    info = fullName.Split(',');
                    if (publicKey != info[3].Substring(info[3].LastIndexOf('=') + 1))
                    {
                        // Assembly has wrong public key
                        continue;
                    }
                }
                names.Add(fullName);
            }
            if (names.Count == 0)
            {
                return(null);
            }
            string  best        = null;
            Version bestVersion = null;
            Version currentVersion;

            if (requiredVersion != null)
            {
                // use assembly with lowest version higher or equal to required version
                for (int i = 0; i < names.Count; i++)
                {
                    info           = names[i].Split(',');
                    currentVersion = new Version(info[1].Substring(info[1].LastIndexOf('=') + 1));
                    if (currentVersion.CompareTo(requiredVersion) < 0)
                    {
                        continue; // version not good enough
                    }
                    if (best == null || currentVersion.CompareTo(bestVersion) < 0)
                    {
                        bestVersion = currentVersion;
                        best        = names[i];
                    }
                }
                if (best != null)
                {
                    return(new AssemblyName(best));
                }
            }
            // use assembly with highest version
            best        = names[0];
            info        = names[0].Split(',');
            bestVersion = new Version(info[1].Substring(info[1].LastIndexOf('=') + 1));
            for (int i = 1; i < names.Count; i++)
            {
                info           = names[i].Split(',');
                currentVersion = new Version(info[1].Substring(info[1].LastIndexOf('=') + 1));
                if (currentVersion.CompareTo(bestVersion) > 0)
                {
                    bestVersion = currentVersion;
                    best        = names[i];
                }
            }
            return(new AssemblyName(best));
        }