Ejemplo n.º 1
0
        Assembly AssemblyResolve(object sender, ResolveEventArgs e)
        {
            AssemblyName name = new AssemblyName(e.Name);

            LoggingService.Debug("ProjectContentRegistry.AssemblyResolve " + e.Name);
            string path = Path.Combine(lookupDirectory, name.Name);

            if (File.Exists(path + ".dll"))
            {
                return(Assembly.ReflectionOnlyLoadFrom(path + ".dll"));
            }
            if (File.Exists(path + ".exe"))
            {
                return(Assembly.ReflectionOnlyLoadFrom(path + ".exe"));
            }
            if (File.Exists(path))
            {
                return(Assembly.ReflectionOnlyLoadFrom(path));
            }
            try {
                LoggingService.Debug("AssemblyResolve trying ReflectionOnlyLoad");
                return(Assembly.ReflectionOnlyLoad(e.Name));
            } catch (FileNotFoundException) {
                LoggingService.Warn("AssemblyResolve: ReflectionOnlyLoad failed for " + e.Name);
                // We can't get the assembly we want.
                // But propably we can get a similar version of it.
                DomAssemblyName fixedName = GacInterop.FindBestMatchingAssemblyName(e.Name);
                LoggingService.Info("AssemblyResolve: FixedName: " + fixedName);
                return(Assembly.ReflectionOnlyLoad(fixedName.FullName));
            }
        }
        static string GetVersion__Token(DomAssemblyName asmName)
        {
            StringBuilder b = new StringBuilder(asmName.Version.ToString());

            b.Append("__");
            b.Append(asmName.PublicKeyToken);
            return(b.ToString());
        }
		public ReflectionProjectContent(Assembly assembly, string assemblyLocation, ProjectContentRegistry registry)
			: this(assembly.FullName, assemblyLocation, DomAssemblyName.Convert(assembly.GetReferencedAssemblies()), registry)
		{
			foreach (Type type in assembly.GetExportedTypes()) {
				string name = type.FullName;
				if (name.IndexOf('+') < 0) { // type.IsNested
					AddClassToNamespaceListInternal(new ReflectionClass(assemblyCompilationUnit, type, name, null));
				}
			}
			InitializeSpecialClasses();
			AddAssemblyAttributes(assembly);
			assemblyCompilationUnit.Freeze();
		}
Ejemplo n.º 4
0
            public ReflectionProjectContent ReadProjectContent(ProjectContentRegistry registry)
            {
                Int64 bigIntFileMagic = reader.ReadInt64();

                if (bigIntFileMagic != FileMagic)
                {
                    LoggingService.Warn("Read dom: wrong magic");
                    return(null);
                }
                if (reader.ReadInt16() != FileVersion)
                {
                    LoggingService.Warn("Read dom: wrong version");
                    return(null);
                }
                string assemblyName     = reader.ReadString();
                string assemblyLocation = reader.ReadString();
                long   time             = 0;

                try
                {
                    time = File.GetLastWriteTimeUtc(assemblyLocation).ToFileTime();
                }
                catch
                {}

                Int64 bigIntTime = reader.ReadInt64();

                //if (bigIntTime != time)
                //{
                //    LoggingService.Warn("Read dom: assembly changed since cache was created");
                //    return null;
                //}

                DomAssemblyName[] referencedAssemblies = new DomAssemblyName[reader.ReadInt32()];
                for (int i = 0; i < referencedAssemblies.Length; i++)
                {
                    referencedAssemblies[i] = new DomAssemblyName(reader.ReadString());
                }

                this.pc = new ReflectionProjectContent(assemblyName, assemblyLocation, referencedAssemblies, registry);
                if (ReadClasses())
                {
                    return(pc);
                }
                else
                {
                    LoggingService.Warn("Read dom: error in file (invalid control mark)");
                    return(null);
                }
            }
Ejemplo n.º 5
0
        static string GetAssemblyFile(DomAssemblyName reference, string prefix, string gac)
        {
            var gac_folder = new StringBuilder()
                             .Append(prefix)
                             .Append(reference.Version)
                             .Append("__");

            gac_folder.Append(reference.PublicKeyToken);

            return(Path.Combine(
                       Path.Combine(
                           Path.Combine(gac, reference.ShortName), gac_folder.ToString()),
                       reference.ShortName + ".dll"));
        }
Ejemplo n.º 6
0
        public AssemblyReferenceModel(IAssemblyModel parentAssemblyModel, DomAssemblyName assemblyName)
        {
            if (parentAssemblyModel == null)
            {
                throw new ArgumentNullException("parentAssemblyModel");
            }
            if (assemblyName == null)
            {
                throw new ArgumentNullException("assemblyName");
            }

            this.parentAssemblyModel = parentAssemblyModel;
            this.assemblyName        = assemblyName;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the file name for an assembly stored in the GAC.
        /// </summary>
        public static string FindAssemblyInNetGac(DomAssemblyName reference)
        {
            // without public key, it can't be in the GAC
            if (reference.PublicKeyToken == null)
            {
                return(null);
            }

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < gacs.Length; j++)
                {
                    var gac  = Path.Combine(gac_paths [i], gacs [j]);
                    var file = GetAssemblyFile(reference, prefixes [i], gac);
                    if (File.Exists(file))
                    {
                        return(file);
                    }
                }
            }

            return(null);
        }
        public static DomAssemblyName FindBestMatchingAssemblyName(DomAssemblyName name)
        {
            string[] info;
            string   version   = name.Version;
            string   publicKey = name.PublicKeyToken;

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

            Fusion.CreateAssemblyNameObject(out assemblyName, name.ShortName, 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 (version != null)
            {
                // use assembly with lowest version higher or equal to required version
                Version requiredVersion = new Version(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 DomAssemblyName(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 DomAssemblyName(best));
        }
Ejemplo n.º 9
0
        IProjectContent GetExistingProjectContent(IProjectContent[] existingProjectContents, DomAssemblyName fullAssemblyName)
        {
            IProjectContent content = registry.GetExistingProjectContent(fullAssemblyName);

            if (content != null)
            {
                return(content);
            }
            else if (existingProjectContents.Any())
            {
                return(GetExistingProjectContentForShortName(existingProjectContents, fullAssemblyName.ShortName));
            }
            return(null);
        }
        protected virtual IProjectContent LoadProjectContent(string itemInclude, string itemFileName)
        {
            string shortName = itemInclude;
            int    pos       = shortName.IndexOf(',');

            if (pos > 0)
            {
                shortName = shortName.Substring(0, pos);
            }

            Assembly assembly           = GetDefaultAssembly(shortName);
            ReflectionProjectContent pc = null;

            if (assembly != null)
            {
                if (persistence != null)
                {
                    pc = persistence.LoadProjectContentByAssemblyName(assembly.FullName);
                }
                if (pc == null)
                {
                    pc = new ReflectionProjectContent(assembly, this);
                    if (persistence != null)
                    {
                        persistence.SaveProjectContent(pc);
                    }
                }
            }
            else
            {
                // find real file name for cecil:
                if (File.Exists(itemFileName))
                {
                    if (persistence != null)
                    {
                        pc = persistence.LoadProjectContentByAssemblyName(itemFileName);
                    }
                    if (pc == null)
                    {
                        pc = CecilReader.LoadAssembly(itemFileName, this);

                        if (persistence != null)
                        {
                            persistence.SaveProjectContent(pc);
                        }
                    }
                }
                else
                {
                    DomAssemblyName asmName = GacInterop.FindBestMatchingAssemblyName(itemInclude);
                    if (persistence != null && asmName != null)
                    {
                        //LoggingService.Debug("Looking up in DOM cache: " + asmName.FullName);
                        pc = persistence.LoadProjectContentByAssemblyName(asmName.FullName);
                    }
                    if (pc == null && asmName != null)
                    {
                        string subPath = Path.Combine(asmName.ShortName, GetVersion__Token(asmName));
                        subPath = Path.Combine(subPath, asmName.ShortName + ".dll");
                        foreach (string dir in Directory.GetDirectories(GacInterop.GacRootPath, "GAC*"))
                        {
                            itemFileName = Path.Combine(dir, subPath);
                            if (File.Exists(itemFileName))
                            {
                                pc = CecilReader.LoadAssembly(itemFileName, this);
                                if (persistence != null)
                                {
                                    persistence.SaveProjectContent(pc);
                                }
                                break;
                            }
                        }
                    }
                    if (pc == null)
                    {
                        HostCallback.ShowAssemblyLoadErrorInternal(itemFileName, itemInclude, "Could not find assembly file.");
                    }
                }
            }
            return(pc);
        }
 public IProjectContent GetExistingProjectContent(DomAssemblyName assembly)
 {
     return(GetExistingProjectContent(assembly.FullName));
 }