Пример #1
0
        /// <summary>
        ///     Modifies a given assembly prior to being loaded from disk.
        /// </summary>
        /// <param name="assemblyFile">The filename of the target assembly.</param>
        /// <returns>A valid assembly.</returns>
        public override Assembly Load(string assemblyFile)
        {
            var targetAssembly = AssemblyDefinition.ReadAssembly(assemblyFile);

            // Strongly-named assemblies cannot be modified
            if (targetAssembly.Name.HasPublicKey)
            {
                return(base.Load(assemblyFile));
            }

            var assemblyFileName = Path.GetFileNameWithoutExtension(assemblyFile);

            var pdbFile    = string.Format("{0}.pdb", assemblyFileName);
            var hasSymbols = File.Exists(pdbFile);

            if (PdbLoader != null && hasSymbols)
            {
                PdbLoader.LoadSymbols(targetAssembly);
            }

            foreach (var action in AssemblyWeavers)
            {
                action(targetAssembly);

                // Verify the assembly at every step
                if (AssemblyVerifier == null)
                {
                    continue;
                }

                AssemblyVerifier.Verify(targetAssembly);
            }

            var memoryStream = new MemoryStream();

            if (PdbLoader != null && hasSymbols)
            {
                PdbLoader.SaveSymbols(targetAssembly);
            }

            // Save the modifed assembly
            targetAssembly.Save(memoryStream);

            if (PdbLoader == null || !hasSymbols)
            {
                return(targetAssembly.ToAssembly());
            }

            var pdbBytes = File.ReadAllBytes(pdbFile);

            return(PdbLoader.LoadAssembly(memoryStream.ToArray(), pdbBytes));
        }