> But why would you want to parse and replace instructions as you're in
> control of what you emit in the MethodBuilder? Just emit what you want
> in a first place.

I'm not emitting the instructions one by one. I'm simply taking the
instructions of a pre-existing method and replacing the ldfld ones. Of
course, I can emit them one by one, that isn't a problem. But I need
to know what the original method is doing to begin with. That is, I
need to take its MethodBody, get the byte array, and know to which
instructions it corresponds. I want to do this (pseudocode):

   foreach (Instruction instruction in originalMethodBody)
       if (instruction is a ldfld instruction) {  <--- need to be able
to recognise a ldfld instruction
           FieldInfo fInfo = instruction.Operand; <--- need to be able
to extract the operand
           ilGenerator.Emit(callvirt, the corresponding method);
       } else {
           ilGenerator.Emit(instruction);
       }

Parsing the instructions may possibly be trivial... I think they have
a fixed number of operands? In that case it should be very easy. I
just didn't want to take care of the parsing myself. Also, I don't
know how efficient it is to emit the instructions one by one as
opposed to replacing a few.

Any comments?

> > In a semi-unrelated question: how do you create a type with Cecil? I'd
> > like to take a pre-existing type and create a type that extends it
> > (i.e., a subclass). I don't want to replace the pre-existing type or
> > the assembly, nor write the assembly to disk. Is it possible?
>
> Cecil can read and write assemblies. And in between, it lets you
> modify them. So if you want to create a new type, you have to do it in
> a new assembly. And you have to load this assembly, either by loading
> using Assembly.Load(byte[]) or LoadFile/From.
>
> You can have a look 
> athttp://github.com/jbevain/cecil/blob/master/Test/Mono.Cecil.Tests/Imp...
> for an example.

Thanks, I will.

-- 
--
mono-cecil

Reply via email to