Exemplo n.º 1
0
        /// <summary>
        /// Returns the original location of the corresponding assembly if available, otherwise returns the location of the shadow copy.
        /// If the corresponding assembly is not in the GAC, null is returned.
        /// </summary>
        public static string /*?*/ GetLocation(AssemblyIdentity assemblyIdentity, IMetadataHost metadataHost)
        {
            lock (GlobalLock.LockingObject) {
#if COMPACTFX
                var gacKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"\Software\Microsoft\.NETCompactFramework\Installer\Assemblies\Global");
                foreach (var gacName in gacKey.GetValueNames())
                {
                    if (IdentityMatchesString(assemblyIdentity, gacName))
                    {
                        var values = gacKey.GetValue(gacName) as string[];
                        if (values == null || values.Length == 0)
                        {
                            continue;
                        }
                        return(values[0]);
                    }
                }
                return(null);
#elif COREFX_SUBSET
                return(null);
#else
#if __MonoCS__
                IAssemblyEnum assemblyEnum = new MonoAssemblyEnum();
#else
                if (!GlobalAssemblyCache.FusionLoaded)
                {
                    GlobalAssemblyCache.FusionLoaded = true;
                    var systemAssembly         = typeof(object).Assembly;
                    var systemAssemblyLocation = systemAssembly.Location;
                    var dir = Path.GetDirectoryName(systemAssemblyLocation) ?? "";
                    GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll"));
                }
                IAssemblyEnum assemblyEnum;
                int           rc = CreateAssemblyEnum(out assemblyEnum, null, null, ASM_CACHE.GAC, 0);
                if (rc < 0 || assemblyEnum == null)
                {
                    return(null);
                }
#endif
                IApplicationContext applicationContext;
                IAssemblyName       currentName;
                while (assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0)
                {
                    //^ assume currentName != null;
                    AssemblyName cn = new AssemblyName(currentName);
                    if (assemblyIdentity.Equals(new AssemblyIdentity(metadataHost.NameTable.GetNameFor(cn.Name), cn.Culture, cn.Version, cn.PublicKeyToken, "")))
                    {
                        string codeBase = cn.CodeBase;
                        if (codeBase != null && codeBase.StartsWith("file://", StringComparison.OrdinalIgnoreCase))
                        {
                            Uri u = new Uri(codeBase, UriKind.Absolute);
                            return(u.LocalPath);
                        }
                        return(cn.GetLocation());
                    }
                }
                return(null);
#endif
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Determines whether the GAC contains the specified code base URI.
 /// </summary>
 /// <param name="codeBaseUri">The code base URI.</param>
 public static bool Contains(Uri codeBaseUri)
 {
     if (codeBaseUri == null)
     {
         Debug.Fail("codeBaseUri == null"); return(false);
     }
     lock (GlobalLock.LockingObject)
     {
         if (!GlobalAssemblyCache.s_fusionLoaded)
         {
             GlobalAssemblyCache.s_fusionLoaded = true;
             System.Reflection.Assembly systemAssembly = typeof(object).Assembly;
             //^ assume systemAssembly != null;
             string /*?*/ systemAssemblyLocation = systemAssembly.Location;
             //^ assume systemAssemblyLocation != null;
             string dir = Path.GetDirectoryName(systemAssemblyLocation);
             //^ assume dir != null;
             GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll"));
         }
         IAssemblyEnum assemblyEnum;
         int           rc = GlobalAssemblyCache.CreateAssemblyEnum(out assemblyEnum, null, null, ASM_CACHE.GAC, 0);
         if (rc < 0 || assemblyEnum == null)
         {
             return(false);
         }
         IApplicationContext applicationContext;
         IAssemblyName       currentName;
         while (assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0)
         {
             //^ assume currentName != null;
             AssemblyName assemblyName = new AssemblyName(currentName);
             string /*?*/ scheme       = codeBaseUri.Scheme;
             if (scheme != null && assemblyName.CodeBase.StartsWith(scheme))
             {
                 try
                 {
                     Uri foundUri = new Uri(assemblyName.CodeBase);
                     if (codeBaseUri.Equals(foundUri))
                     {
                         return(true);
                     }
                 }
                 catch (System.ArgumentNullException)
                 {
                 }
                 catch (System.UriFormatException)
                 {
                 }
             }
         }
         return(false);
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Returns the original location of the corresponding assembly if available, otherwise returns the location of the shadow copy.
 /// If the corresponding assembly is not in the GAC, null is returned.
 /// </summary>
 public static string /*?*/ GetLocation(AssemblyIdentity assemblyIdentity, IMetadataHost metadataHost)
 {
     lock (GlobalLock.LockingObject)
     {
         if (!GlobalAssemblyCache.s_fusionLoaded)
         {
             GlobalAssemblyCache.s_fusionLoaded = true;
             System.Reflection.Assembly systemAssembly = typeof(object).Assembly;
             //^ assume systemAssembly != null;
             string /*?*/ systemAssemblyLocation = systemAssembly.Location;
             //^ assume systemAssemblyLocation != null;
             string dir = Path.GetDirectoryName(systemAssemblyLocation);
             //^ assume dir != null;
             GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll"));
         }
         IAssemblyEnum assemblyEnum;
         CreateAssemblyEnum(out assemblyEnum, null, null, ASM_CACHE.GAC, 0);
         if (assemblyEnum == null)
         {
             return(null);
         }
         IApplicationContext applicationContext;
         IAssemblyName       currentName;
         while (assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0)
         {
             //^ assume currentName != null;
             AssemblyName assemblyName = new AssemblyName(currentName);
             string /*?*/ location     = assemblyName.GetLocation();
             if (location == null)
             {
                 location = string.Empty;
             }
             if (assemblyIdentity.Equals(new AssemblyIdentity(metadataHost.NameTable.GetNameFor(assemblyName.Name), assemblyName.Culture, assemblyName.Version, assemblyName.PublicKeyToken, location)))
             {
                 string codeBase = assemblyName.CodeBase;
                 if (codeBase != null && codeBase.StartsWith("file:///"))
                 {
                     return(codeBase.Substring(8));
                 }
                 return(assemblyName.GetLocation());
             }
         }
         return(null);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Determines whether the GAC contains the specified code base URI.
        /// </summary>
        /// <param name="codeBaseUri">The code base URI.</param>
        public static bool Contains(Uri codeBaseUri)
        {
            if (codeBaseUri == null)
            {
                Debug.Fail("codeBaseUri == null"); return(false);
            }
            lock (GlobalLock.LockingObject) {
#if COMPACTFX
                var gacKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"\Software\Microsoft\.NETCompactFramework\Installer\Assemblies\Global");
                if (gacKey == null)
                {
                    return(false);
                }
                var codeBase = codeBaseUri.AbsoluteUri;
                foreach (var gacName in gacKey.GetValueNames())
                {
                    var values = gacKey.GetValue(gacName) as string[];
                    if (values == null || values.Length == 0)
                    {
                        continue;
                    }
                    if (string.Equals(values[0], codeBase, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                    if (values.Length > 1 && string.Equals(values[1], codeBase, StringComparison.OrdinalIgnoreCase))
                    {
                        return(true);
                    }
                }
                return(false);
#else
                if (!GlobalAssemblyCache.FusionLoaded)
                {
                    GlobalAssemblyCache.FusionLoaded = true;
                    System.Reflection.Assembly systemAssembly = typeof(object).Assembly;
                    //^ assume systemAssembly != null;
                    string /*?*/ systemAssemblyLocation = systemAssembly.Location;
                    //^ assume systemAssemblyLocation != null;
                    string dir = Path.GetDirectoryName(systemAssemblyLocation);
                    //^ assume dir != null;
                    GlobalAssemblyCache.LoadLibrary(Path.Combine(dir, "fusion.dll"));
                }
                IAssemblyEnum assemblyEnum;
                int           rc = GlobalAssemblyCache.CreateAssemblyEnum(out assemblyEnum, null, null, ASM_CACHE.GAC, 0);
                if (rc < 0 || assemblyEnum == null)
                {
                    return(false);
                }
                IApplicationContext applicationContext;
                IAssemblyName       currentName;
                while (assemblyEnum.GetNextAssembly(out applicationContext, out currentName, 0) == 0)
                {
                    //^ assume currentName != null;
                    AssemblyName assemblyName = new AssemblyName(currentName);
                    string /*?*/ scheme       = codeBaseUri.Scheme;
                    if (scheme != null && assemblyName.CodeBase.StartsWith(scheme))
                    {
                        try {
                            Uri foundUri = new Uri(assemblyName.CodeBase);
                            if (codeBaseUri.Equals(foundUri))
                            {
                                return(true);
                            }
                        } catch (System.ArgumentNullException) {
                        } catch (System.UriFormatException) {
                        }
                    }
                }
                return(false);
#endif
            }
        }