6

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:

  1. Is it not possible to access the underlying fields of the generics?
  2. If it is possible, what would the import statement look like for 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").

4

1 回答 1

3

在写入指向它的 IL 之前,您需要将 FieldDefinition 导入 ModuleDefinition。

因此,在查看您的代码之后,它将是这样的。

var fieldReference = ModuleDefinition.Import(field);
Action<Collection<Instruction>> load = collection => collection.AddI(OpCodes.Ldfld, fieldReference);

我还注意到您还有另一个错误。当您在上面的代码中时,您已经丢失了类型参数的上下文。所以你试图调用一些东西List<T>而不是像List<MyClass>. 但是如果你不能解决那个问题,你可以提出另一个 SO 问题:)

于 2013-05-01T10:50:09.633 回答