在IL中查找方法调用的参数值
我有几个特殊的方法,我想分析它们在编译的程序集中的调用。
示例:
public static class SrcHelper {
[MySpecialMethod]
[Conditional( "DEBUG" )]
public static void ToDo( params object[] info ) {
/* do nothing */
/* this method is not called when code is compiled in RELEASE mode */
}
}
// ... somewhere else in another assembly ...
Array CreateArraySampleMethod( int size ) {
// This call has only informative character. No functionality is required.
SrcHelper.ToDo( "Should create array of ", typeof( MyClass ), " with specified size." );
throw new NotImplementedException();
}
从这个编译的代码中,我想获取参数值 {“应该创建具有指定大小的“,MyClass,”数组。” }。 我尝试使用 Mono 中的 Cecil,并找到了调用“ToDo”方法的说明。 但现在我很困惑如何识别带有参数值的指令。
我知道,情况可能很复杂,有些争论的价值无法得到解决。 但我只需要解析恒定值 - 这足以满足我的目的。
谢谢。
编辑: “ToDo”方法(和类似的方法)应该用作注释( //, /* ... */ )的替代方法,并且在编译后,应该进行 IL 分析并自动生成文档和待办事项列表以进行具体组装。
I have several special methods, and I want analyze they calls in compiled assembly.
Example:
public static class SrcHelper {
[MySpecialMethod]
[Conditional( "DEBUG" )]
public static void ToDo( params object[] info ) {
/* do nothing */
/* this method is not called when code is compiled in RELEASE mode */
}
}
// ... somewhere else in another assembly ...
Array CreateArraySampleMethod( int size ) {
// This call has only informative character. No functionality is required.
SrcHelper.ToDo( "Should create array of ", typeof( MyClass ), " with specified size." );
throw new NotImplementedException();
}
From this compiled code I want get the argument values { "Should create array of ", MyClass, " with specified size." }.
I tried use Cecil from Mono, and I found the instructions for call "ToDo" method. But now am I confused how to identify instruction with argument values.
I know, there can be complex situation, and some argument's value can not be resolved. But I need resolve only constant values - it's enough for my purpose.
Thanks.
EDIT:
The "ToDo" method (and similar ones) should be used as alternative to comments ( //, /* ... */ ), and after compilation, should be IL analyzed and autogenerated documentation and todo-list for concrete assembly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码生成有点令人困惑,但可以在简单的情况下完成:
编译:(
添加“x”以强制其使用参数重载)
给出
所以您要做的就是解析 il 以检测被推入编译器生成的参数大批。 这是一种脆弱的断言,但可能足以说明:
这很粗糙,但可能足以满足您的需求。
AOP 风格的方法将通过简单地检测每个调用以转储值来在运行时为您提供所需的内容,但在编译时,上面的方法是仅给出 IL 的唯一现实选择。
显式创建的数组(可能距离调用站点更远,甚至在不同的方法/构造函数/类中)
。
生成的代码在发布版本中可能非常不同,您将无法发现自动生成的数组与某人自己 编辑后应注意为什么要这样做,基于属性的注释是一个更好的解决方案,我不明白为什么当您可以直接属性时要在方法中执行此操作...
The code generation is somewhat confusing but can be done for simple cases:
compiling:
(adding "x" to force it to use a params overload)
gives
So you have to do is parse the il to detect the arguments being pushed into the compiler generated array. a heristic that is fragile but might be sufficient it to say:
This is rough but may be sufficient for your needs.
An AOP style approach will get you what you want at runtime by simply instrumenting every call to dump the values but at sompile time the approach above is your only realistic option given only the IL.
The code generated may be very different in Release builds, You will be unable to spot the auto generated array verses someone explicitly creating it themselves (which may be further away from the call site or even in a different method/constructor/class.
Proviso
I should note after your edit for why you want to do this that Attribute based annotations are a far better solution, I cannot see why you would want to do this in the method when you can attribute it directly...