jbevain / cecil

Cecil is a library to inspect, modify and create .NET programs and libraries.
MIT License
2.73k stars 624 forks source link

Problem with weawing #911

Open tdsrhh opened 1 year ago

tdsrhh commented 1 year ago

Hi

I am trying to remove some embedded resources.

// the list TmpRemove contains the list of resources to remove ...

        IEnumerable<MethodDefinition> TmpMethods
            = Asm.MainModule.Types.Where(o => o.IsClass == true && o.Name == "AssemblyLoader")
                                               .SelectMany(type => type.Methods)
                                               .Where(o => o.FullName.Contains("cctor"));

        MethodDefinition TmpMethod
            = null;

        if (TmpMethods != null && TmpMethods.Count() > 0)
            TmpMethod = TmpMethods.First();

        List<Instruction> TmpInstruction
            = new List<Instruction>();

        foreach (Resource res in TmpRemove)
        {
            Asm.MainModule.Resources.Remove(res);

            if (!res.Name.StartsWith("costura.tds.") && TmpMethod != null)
            {
                for (int i = 0; i < TmpMethod.Body.Instructions.Count; i++)
                {
                    if (TmpMethod.Body.Instructions[i].Operand != null && TmpMethod.Body.Instructions[i].Operand.Equals(res.Name))
                    {
                        TmpMethod.Body.SimplifyMacros();

                        TmpInstruction.Add(TmpMethod.Body.Instructions[i - 2]);
                        TmpInstruction.Add(TmpMethod.Body.Instructions[i - 1]);
                        TmpInstruction.Add(TmpMethod.Body.Instructions[i]);
                        TmpInstruction.Add(TmpMethod.Body.Instructions[i + 1]);

                        TmpMethod.Body.OptimizeMacros();
                    }
                }
            }
        }

        foreach (Instruction ins in TmpInstruction)
            TmpMethod.Body.Instructions.Remove(ins);

This works fine, the resources are removed from my dll.

But I have a problem with the PDB file not getting updated correctly :

        using (AssemblyDefinition TmpAssembly = AssemblyDefinition.ReadAssembly(TargetPath, new ReaderParameters { SymbolReaderProvider = new PdbReaderProvider(), ReadSymbols = true, ReadWrite = true }))
        {
            MemoryStream ms = null;
            Find(true, TmpExport, TmpAssembly, ref ms);

            TmpAssembly.Write(new WriterParameters()
            {
                WriteSymbols
                    = true,

                SymbolWriterProvider
                    = new Mono.Cecil.Pdb.PdbWriterProvider(),
            });
        }

I get this error -

1> Non-negative number required. 1> Parameter name: count

Any help is apprishiated .Anyone ?

Regards