ILGenerator 是否有一个好的包装器?

发布于 2024-07-07 17:17:07 字数 1051 浏览 17 评论 0原文

我现在使用 System.Reflection.Emit 一段时间了,发现它(谁不这样做?)就像容易出现错误一样痛苦。

您知道 IL 生成器是否有一个好的包装器,我可以依靠它以比直接使用 SRE 更安全、更简单的方式发出 IL?

编辑:

我知道操作表达式树绝对比直接发出 IL 更容易、更安全,但它们现在也有一些限制。 我无法创建代码块、使用循环、声明并使用多个局部变量等。我们需要等到 .NET 4 发布:)

此外,我正在处理一个已经依赖于 SRE 的代码库。

显然,ILGenerator 可以满足我需要的一切。 但在操作它时我希望得到更多帮助。 当我提到 ILGenerator 包装器(它仍然处于相当低的水平)时,我想到了可以提供以下方法的东西:

// Performs a virtual or direct call on the method, depending if it is a 
// virtual or a static one.
Call(MethodInfo methodInfo)

// Pushes the default value of the type on the stack, then emit 
// the Ret opcode.
ReturnDefault(Type type)

// Test the object type to emit the corresponding push 
// opcode (Ldstr, Ldc_I*, Ldc_R*, etc.)
LoadConstant(object o)

这确实是 3 个幼稚的示例,但它足以证明我的期望。 我们可以将其视为一组扩展方法,但最好支持条件语句和循环,例如 RunSharp。 事实上,RunSharp 非常接近我想要的,但它对 ILGenerator 进行了过多的抽象,并且没有公开其所有功能。

我不记得在哪里了,但我已经在一个开源项目中看到过这样的助手。

I'm using System.Reflection.Emit for a while now, and find it (who don't?) as painful as bug prone.

Do you know if there is a good wrapper around the IL Generator, something that I can rely on to emit IL in a more safe and easier manner than with playing directly with SRE?

Edit:

I know that manipulating expression trees is definitively easier and safer than emitting IL directly, but they also have some constraints right now. I can't create code blocs, use loops, declare and work with several locals, etc. We need to wait until .NET 4 comes out :)

Moreover, I'm dealing with a code base which already relies on SRE.

Obviously, ILGenerator do everything I need. But I would appreciate more assistance when manipulating it. When I'm referring to a ILGenerator wrapper, which remains at a pretty low level, I think about something which could provide methods like:

// Performs a virtual or direct call on the method, depending if it is a 
// virtual or a static one.
Call(MethodInfo methodInfo)

// Pushes the default value of the type on the stack, then emit 
// the Ret opcode.
ReturnDefault(Type type)

// Test the object type to emit the corresponding push 
// opcode (Ldstr, Ldc_I*, Ldc_R*, etc.)
LoadConstant(object o)

It's really 3 naive examples, but it could be enough to demonstrate what I expect. We can see that as a set of extension methods, but it could be nice to have support for conditional statements and loops like in RunSharp. In fact, RunSharp is pretty close that what I want, but it abstracts the ILGenerator too much and doesn't expose all its functionality.

I can't remember where, but I already saw such an helper in an open source project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘虞初梦 2024-07-14 17:17:07

如果您使用 .NET 3.5,您可能会发现使用表达式树更为合理。 这完全取决于你在做什么——而且它仍然可能非常痛苦——但这肯定是另一个需要注意的选择。

If you're using .NET 3.5, you may find using Expression Trees to be more reasonable. It entirely depends on what you're doing - and it can still be quite painful - but it's certainly another option to be aware of.

最单纯的乌龟 2024-07-14 17:17:07

[更新]:我想到了这个名字 ;-p RunSharp。 我不能保证它,但它可能正是你所需要的。

然而; 你需要生成什么? CodeDom 是一种选择。 对于创建方法,您可能会发现使用 Expression 类(通过 Expression.Lambda/Compile 将其编译为类型化委托后)。

[updated]: I thought of the name ;-p RunSharp. I can't vouch for it, but it might be what you need.

However; what do you need to generate? CodeDom is one option. For creating methods, you might find that you can do a lot more than you expect with the Expression class in .NET 3.5 (after compiling it to a typed delegate via Expression.Lambda/Compile.

另类 2024-07-14 17:17:07

尝试使用 Mono.Cecil

Cecil 是由 Jb Evain 编写的库
生成
并检查程序和库
ECMA CIL 格式。 它有完整的
支持泛型,并支持一些
调试符号格式。

Try using Mono.Cecil

Cecil is a library written by Jb Evain
to generate
and inspect programs and libraries in
the ECMA CIL format. It has full
support for generics, and support some
debugging symbol format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文