Mono.Cecil:从其他程序集中调用基类的方法 [英] Mono.Cecil: call base class' method from other assembly
问题描述
如何通过名称获取对基类的方法的MethodReference?
How can I get a MethodReference to a base class' method by name?
我尝试过
type.BaseType.Resolve().Methods;
,如果我将包含基类的dll添加到assemblyresolver中,它将返回方法. 但是,如果我使用
and if I add the dll containing the base class to the assemblyresolver it returns the methods. But if I add a call using
MSILWorker.Create(OpCodes.Call, baseMethod);
(其中baseMethod是通过解析的TypeDefinition中的foreaching方法找到的) 即使反射器冻结并退出,最终的IL也无法读取.
(where baseMethod was found by foreaching Methods from the resolved TypeDefinition) the resulting IL is unreadable, even Reflector freezes and quits.
现在有一些IL:
如果在类型上调用私有方法
Now some IL:
if calling private method on type:
call instance void SomeNamespace.MyClass::RaisePropertyChanged(string)
如果基于基本类型调用受保护的方法
if calling protected method on base type:
call instance void [OtherAssembly]BaseNamespace.BaseClass::RaisePropertyChanged(string)
那么,如何使用Mono.Cecil生产后者?
So, how can I produce the latter using Mono.Cecil?
推荐答案
您猜到了,您需要获取一个适用于该模块的适当的MethodReference.因此,如果您有:
As you guessed, you need to get a proper MethodReference scoped for the module. So if you have:
TypeDefinition type = ...;
TypeDefintion baseType = type.BaseType.Resolve ();
MethodDefinition baseMethod = baseType.Methods.First (m => ...);
然后baseType和baseMethod是另一个模块的定义.您需要先导入对baseMethod的引用,然后再使用它:
Then baseType and baseMethod are definitions from another module. You need to import a reference to the baseMethod before using it:
MethodReference baseMethodReference = type.Module.Import (baseMethod);
il.Emit (OpCodes.Call, baseMethodReference);
这篇关于Mono.Cecil:从其他程序集中调用基类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!