You have to load the PDB before modifying the assembly and save it to
disc with the assembly after modifying. The code should look something
like this:
AssemblyDefinition cecilAssembly =
AssemblyFactory.GetAssembly(assemblyName);
cecilAssembly.MainModule.LoadSymbols(); // this involves Cecil.Pdb.dll
or Cecil.Mdb.dll, base on what system you'r running it
// modify the assembly
cecilAssembly.MainModule.SaveSymbols();
AssemblyFactory.SaveAssembly(cecilAssembly, assemblyName);
Or at least it works for me like that. I've had some problems with
writing to the files becaue they're locked, though...
The code looks a bit strange as you don't have a direct reference to
Cecil.Pdb.dll or Cecil.Mdb.dll: they are loaded dynamically by Cecil.dll
based on the current system (MS.NET/Mono) to have portable code. The two
assemblies must be put at a place where they can be found by the .NET
runtime.
Simon
Timwi wrote:
> Hi,
>
> I'm trying to get a list of the names of the local variables in a
> specific method.
>
> So I'm using something like this ...
> foreach (var m in myLibrary.Modules.Cast<ModuleDefinition>
> ()) {
> foreach (var t in m.Types.Cast<TypeDefinition>()) {
> foreach (var e in t.Methods.Cast<MethodDefinition>
> ()) {
> foreach (var v in
> e.Body.Variables.Cast<VariableDefinition>()) {
> Console.WriteLine(v.Name);
> }
> }
> }
> }
>
> Unfortunately, I only get "V_0", "V_1", etc., not the real variable
> names.
>
> I tried actually changing the names to something like "a", "b", etc.,
> by assigning to v.Name, but Red Gate's .NET Reflector still shows them
> with their original name, so the name wasn't changed.
>
> Renaming methods by assigning to m.Name appears to work. Reflector
> shows the new names.
>
> Why doesn't this work for variables, and how do I make it work?
>
> Thanks!
> Timwi
> >
>
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---