反射器和编译器生成的代码
我有一个 dll,我在 Reflector 中反汇编它,然后生成一个类。 该类包含无法在 Visual Studio 中编译的代码。
我认为它是 leagl IL 代码,但如何从中生成更高级别的 c# 代码。
似乎yield 和 IEnumerator 在 IL 中生成诸如 <>1__state;
之类的东西,但无法编译。
有谁知道我如何从包含此类 IL 的反射器生成一个类?
有没有反射器插件可以解决这个问题?
private sealed class <Rule_Document>d__0 : IEnumerable<HtmlTag>, IEnumerable, IEnumerator<HtmlTag>, IEnumerator, IDisposable
{
private int <>1__state;
private HtmlTag <>2__current;
public HtmlParser <>4__this;
private int <>l__initialThreadId;
public HtmlTag <htmlTag>5__1;
I have a dll which I disassemble in Reflector, I then generate a class.
The class contains code which does not compile in Visual Studio.
I take it it's leagl IL code, but how can I generate the higher level c# from this.
Seems yield and IEnumerator generate things like <>1__state;
in the IL which will not compile.
Does anybody know how I can generate a class from reflector that contains this sort of IL?
Is there a reflector addin that will solves this?
private sealed class <Rule_Document>d__0 : IEnumerable<HtmlTag>, IEnumerable, IEnumerator<HtmlTag>, IEnumerator, IDisposable
{
private int <>1__state;
private HtmlTag <>2__current;
public HtmlParser <>4__this;
private int <>l__initialThreadId;
public HtmlTag <htmlTag>5__1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该 IL 来自迭代器块。手动编写迭代器可怕,而迭代器块中的底层 IL 证明了编译器为您所做的事情的真实性。 庆幸您不必自己做!
您可以尝试主要通过
MoveNext()
将它们组合在一起,但不要指望奇迹。您可能还会发现重命名字段(以消除诸如<> 前缀之类的内容)会大有帮助。
然而;我期望最好的办法是理解代码想要做什么,并停止尝试简单地
窃取借用< /em> 他们的实现。That IL is from an iterator block. Iterators are horrible to write manually, and the underlying IL from an iterator block is testament to the reality of what the compiler does for you. Be grateful you didn't have to do it yourself!
You could try putting that together mainly from the
MoveNext()
, but don't expect miracles. You may also find that renaming the fields (to get rid of things like<>
prefixes) goes a long way.However; I expect the best thing to do is to understand what the code is trying to do, and stop trying to simply
stealborrow their implementation.您应该查看 HTML Agility Pack。
You should look at the HTML Agility Pack.