Mono Cecil error generating PDB

2019-06-25 10:00发布

问题:

The code below allows me to change methods body and save back the assembly.

        // Recreate PDB
        var assemblyResolver = new DefaultAssemblyResolver();
        var assemblyLocation = Path.GetDirectoryName(pathBin);
        assemblyResolver.AddSearchDirectory(assemblyLocation);
        /*if (!string.IsNullOrEmpty(HintPath))
        {
            assemblyResolver.AddSearchDirectory(HintPath);
        }*/
        var silverlightAssemblyPath = Environment.ExpandEnvironmentVariables(@"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\");
        assemblyResolver.AddSearchDirectory(silverlightAssemblyPath);
        var readerParameters = new ReaderParameters { AssemblyResolver = assemblyResolver };
        var writerParameters = new WriterParameters();
        var pdbName = Path.ChangeExtension(pathBin, "pdb");
        if (createPDB && File.Exists(pdbName))
        {
            var symbolReaderProvider = new PdbReaderProvider();
            readerParameters.SymbolReaderProvider = symbolReaderProvider;
            readerParameters.ReadSymbols = true;
            writerParameters.WriteSymbols = true;
        }
        //Gets the AssemblyDefinition of "MyLibrary"
        AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(pathBin, readerParameters);
        System.IO.File.Delete(pathBin.Replace(".dll", ".back"));
        assembly.Write(pathBin.Replace(".dll", ".back"));

        if (assembly.Modules.SelectMany(m => m.Types.SelectMany(t => t.Methods.Select(meth => new MethodDefinitionEx(assembly, t, meth))))
            .Where(exp)
            .Any())
        {
            //Save
            if (createPDB)
                assembly.Write(pathBin, writerParameters);
            else
                assembly.Write(pathBin);
            return true;
        }
        return false;

But when I ask cecil to create the symbols file (pdb), the created file is quite different from the original and I can't debug the resulting assembly.

I've found this references on google...but nothing works for me.

  • johnhmarks.wordpress.com/2011/01/19/getting-mono-cecil-to-rewrite-pdb-files-to-enable-debugging
  • groups.google.com/forum/#!topic/mono-cecil/s0Dc3GSgsHY
  • github.com/jbevain/cecil/issues/4

Anybody?