ILGenerator周围有一个很好的包装器吗?

时间:2021-06-24 18:49:07

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

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

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?

你知道IL Generator周围是否有一个好的包装器,我可以依赖它以比直接使用SRE更安全,更容易的方式发出IL吗?

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 :)

我知道操纵表达树比直接发射IL更容易和更安全,但它们现在也有一些限制。我不能创建代码块,使用循环,声明和使用几个本地,等等。我们需要等到.NET 4出来:)

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

而且,我正在处理一个已经依赖于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:

显然,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)

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.

这真的是3个天真的例子,但它足以证明我的期望。我们可以看到它是一组扩展方法,但是对于像RunSharp这样的条件语句和循环的支持可能会很好。实际上,RunSharp与我想要的非常接近,但它过多地抽象了ILGenerator并且没有公开它的所有功能。

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

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

3 个解决方案

#1


6  

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.

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

#2


4  

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

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

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.

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

#3


2  

Try using Mono.Cecil

尝试使用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.

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

#1


6  

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.

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

#2


4  

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

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

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.

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

#3


2  

Try using Mono.Cecil

尝试使用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.

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