TDa*_*ver 3 inheritance il cil assemblies mono.cecil
如何按名称获取MethodReference到基类的方法?
我试过了
type.BaseType.Resolve().Methods;
Run Code Online (Sandbox Code Playgroud)
如果我将包含基类的dll添加到assemblyresolver,它将返回方法.但是,如果我使用添加呼叫
MSILWorker.Create(OpCodes.Call, baseMethod);
Run Code Online (Sandbox Code Playgroud)
(通过从解析的TypeDefinition中预处理方法找到baseMethod),生成的IL不可读,甚至Reflector冻结并退出.
现在有些IL:
如果在类型上调用私有方法:
call instance void SomeNamespace.MyClass::RaisePropertyChanged(string)
Run Code Online (Sandbox Code Playgroud)
如果在基类型上调用protected方法:
call instance void [OtherAssembly]BaseNamespace.BaseClass::RaisePropertyChanged(string)
Run Code Online (Sandbox Code Playgroud)
那么,我如何使用Mono.Cecil生成后者呢?
正如您猜测的那样,您需要为模块获取适当的MethodReference作用域.所以如果你有:
TypeDefinition type = ...;
TypeDefintion baseType = type.BaseType.Resolve ();
MethodDefinition baseMethod = baseType.Methods.First (m => ...);
Run Code Online (Sandbox Code Playgroud)
然后baseType和baseMethod是另一个模块的定义.在使用之前,您需要导入对baseMethod的引用:
MethodReference baseMethodReference = type.Module.Import (baseMethod);
il.Emit (OpCodes.Call, baseMethodReference);
Run Code Online (Sandbox Code Playgroud)