Using Mono.Cecil I can iterate over the fields on System.Collections.Generic.List
(_items
, _size
, _version
, etc.), however if I try to use them I always get the exception
Member 'T[] System.Collections.Generic.List`1::_items' is declared in another module and needs to be imported
I have two questions regarding this:
I've successfully accessed private members on objects (as long as they're not compiler generated), so I'm assuming (1) is ok. I've also successfully imported things, although I admit my understanding of how the import is working is shaky (aka "if it gives an error, just try importing it").
You need to import the FieldDefinition into the ModuleDefinition before writing IL that points to it.
So after looking at your code it would be something like this.
var fieldReference = ModuleDefinition.Import(field);
Action<Collection<Instruction>> load = collection => collection.AddI(OpCodes.Ldfld, fieldReference);
I also note that you have another bug. By the time you are in the above code you have lost the context of the type arguments. So you are trying to call something on List<T>
instead of something like List<MyClass>
. But you can raise another SO question if you cant solve that one :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With