【问题标题】:Mono.Cecil: call base class' method from other assemblyMono.Cecil:从其他程序集中调用基类的方法
【发布时间】:2011-02-04 15:31:13
【问题描述】:

如何通过名称获取基类方法的 MethodReference?

我试过了

type.BaseType.Resolve().Methods;

如果我将包含基类的 dll 添加到 assemblyresolver,它会返回方法。 但是,如果我使用

添加呼叫
MSILWorker.Create(OpCodes.Call, baseMethod);

(其中 baseMethod 是通过从已解析的 TypeDefinition 中搜索方法找到的) 生成的 IL 不可读,甚至 Reflector 冻结并退出。

现在一些 IL:
如果在类型上调用私有方法:

 call instance void SomeNamespace.MyClass::RaisePropertyChanged(string)

如果在基类型上调用受保护的方法:

call instance void [OtherAssembly]BaseNamespace.BaseClass::RaisePropertyChanged(string)

那么,如何使用 Mono.Cecil 生成后者?

【问题讨论】:

    标签: inheritance assemblies cil il mono.cecil


    【解决方案1】:

    正如您所猜测的,您需要为模块获取适当的 MethodReference 范围。所以如果你有:

    TypeDefinition type = ...;
    TypeDefintion baseType = type.BaseType.Resolve ();
    MethodDefinition baseMethod = baseType.Methods.First (m => ...);
    

    那么 baseType 和 baseMethod 是来自另一个模块的定义。您需要在使用前导入对 baseMethod 的引用:

    MethodReference baseMethodReference = type.Module.Import (baseMethod);
    il.Emit (OpCodes.Call, baseMethodReference);
    

    【讨论】:

    • 你能帮我解决我的类似问题吗? stackoverflow.com/questions/4968755/… 谢谢。
    • @TDaver:您应该使用 mono-cecil google 组而不是 SO,您将有更好的机会快速收到答案。
    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    相关资源
    最近更新 更多