Comments
3 comments
-
Hi.
If I take a simple class definition with no constructor and compile it, and then view using Reflector, if I have chosen the class, then Reflector displays.public class Class1 { // Methods public Class1(); }
with a link "Expand Methods" at the bottom of the page. This initial view is trying to show you an overview of the methods that are defined. If you click the "Expand methods", then you wil see the codepublic class Class1 { }
which is the code you want to cut and paste, and is the version that the "export" generates.
On the View/Options menu you can control the optimisation. Setting that to "none" gives the viewpublic Class1() { base..ctor(); return; }
This is expected. In this mode, you are supposed to get something that is pretty close to the original IL. In the IL, the C# compiler has generated a default constructor, so this is what you see if you don't optimise. If you set the optimisation to .NET 2, then Reflector spots that the IL code is just the default constructor and then avoids emitting it, giving you the more natural looking C#. -
Clive, what I'm talking about is if you select an entire namespace and right-click -> Disassemble. At the bottom of the window is an Expand Types hyperlink that, when clicked, expands all class definitions.
I've just tried this on a different namespace - that actually has something other than constructors and properties - and it works exactly as you described. But if I select a single class, the hyperlink at the bottom reads Expand Methods, and if I click that, all methods in the class - including ctors - are expanded to show their contents.
I was under the impression that "Expand Types" and "Expand Methods" did the same thing, so I must apologise for the confusion. However, it would be nice if the "namespace view" had an Expand Methods link as well as Expand Types, so I guess this post has turned into a feature request. -
Thanks for the explanation... I'll add that as a feature request.
Add comment
Please sign in to leave a comment.
I then used the latest version of Reflector (6.5.0.135) to inspect the DLL and noticed that Reflector was implicitly generating public no-arg constructors for all classes. That's fine, but instead of generating them as expected:
it's generating the following:
Hence, if I expand all classes, copy them into a new .cs file, and try to compile it, I get a compile error.
Furthermore, if I right-click the assembly in Reflector and choose Export..., C# code files are generated as expected, and they also have implicit constructors - but these constructors are completely different to the ones generated when viewing code in Reflector:
So my questions are:
1. Why is Reflector explicitly generating code for implicit public no-args constructors?
2. Why is different code being generated when I view the assembly, as opposed to exporting it to C# source files?
3. Why do the explictly-generated constructors in the exported C# source explictly call the base class's constructor, and why do they have"return" statements at the end?