4

下面的代码允许我更改方法主体并保存回程序集。

        // 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;

但是当我让 cecil 创建符号文件 (pdb) 时,创建的文件与原始文件完全不同,我无法调试生成的程序集。

我在谷歌上找到了这个参考资料......但对我没有任何作用。

  • 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

有人吗?

4

0 回答 0