views:

227

answers:

2

I've just disassembled a project to debug it using Reflector, but it seems to balk at decoding the 'compile results' of automatic properties, e.g. the next line gives me a syntax error. I've tried fixing these manually, but every time I fix one, more appear.

private string <GLDescription>k__BackingField;

Is there anything I can do about this?

+2  A: 

The compiler generates fields with "unspeakable names" - i.e. ones which are illegal in C# itself, but are valid IL.

There's no exactly accurate translation of the IL into "normal" C# (without automatic properties). You can replace < and > with _ which will give legal code, but then of course it won't be exactly the same code any more. If you're only after the ability to debug, however, that won't be a problem.

If you decompile iterators (i.e. methods using yield statements) you'll find more of the same, including the use of fault blocks, which are like finally blocks but they only run when an exception has occurred (but without catching the exception). Various other constructs generate unspeakable names too, including anonymous methods, lambda expressions and anonymous types.

On a broader note, do you have permission to decompile this code? If the author doesn't mind you doing so, they're likely to be willing to give you the source code to start with which would make your life easier. If they don't want you debugging their source code to start with, you should consider the ethical (and potentially legal) ramifications of decompiling the code. This may vary by location: consult a real lawyer for more definitive guidance.

EDIT: Having seen your own answer, that makes a lot of sense. I'll leave this here for background material.

Jon Skeet
So it's basically Goodbye Yellow-Brick for decompiling :-( I just ran it again using 3.5 optimization, and it actually decodes auto-properties, but everything else is a really ugly mess.
ProfK
Yellow-Brick Road, that is.
ProfK
Right, there will always be some things that Reflector can't piece back together accurately. It's great for looking at what something does, but not for creating compilable source code in every situation. Of course you can fix things up by hand in some cases, but I'd try to avoid requiring it, to be honest.
Jon Skeet
@John, you are right in raising the permissions issue, but I'm only decompiling the library to try and find a problem in my calling code. The author is not available now. However, I see the code has been obfuscated, so I'll have to carry on guessing while I wait until morning.
ProfK
+2  A: 

Ha! Stupid me: all I had to do was set the disassembler optimization in Reflector's options to .NET 3.5. Mine was on 2.0.

ProfK