2013-04-09 35 views
1

我做了一下IL與Mono.Cecil能編織,而且我遇到這個問題:Mono.Cecil能導入列表枚舉

Member 'System.Collections.Generic.List`1/Enumerator' is declared in 
another module and needs to be imported 

你如何去有關導入具有模塊列表枚舉?

我有一個TypeReference這我使用得到一個枚舉,像這樣(System.Collections.Generic.List`1 <等等>):

var instanceType = (typeReference as GenericInstanceType); 
var list = instanceType.Resolve(); 

MethodDefinition getEnumerator; 
if (!list.TryGetMethod("GetEnumerator", out getEnumerator)) 
    throw ... 

...其中TryGetMethod是一個自定義用於搜索具有該名稱的方法的擴展。

然後我用的GetEnumerator進一步下跌的代碼,像這樣:

instructions.Add(Instruction.Create(OpCodes.Callvirt, getEnumerator)); 

我在做什麼錯?

回答

1

我想通了。要獲得列表的列表,您需要獲得MethodReferenceGetEnumerator方法,如下所示:

Type listType = typeof (List<>); 

MethodReference getEnumerator = moduleDefinition 
    .Import(listType.GetMethod("GetEnumerator"));