mike mike - 1 month ago 19
C# Question

Reflector and compiler generated code

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;

Answer

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 steal borrow their implementation.

Comments