Bug: implicit no-arg constructors generated incorrectly?

Explore, browse and analyze .NET assemblies

Moderators: Luke Jefferson, Charles Brown, StephenC, Alex.Davies, Greg.Tillman, melvyn.harbour

Bug: implicit no-arg constructors generated incorrectly?

Postby The_Assimilator » Wed Nov 03, 2010 3:39 pm

I have used xsd.exe to generate a .cs file from a .xsd document, and then compiled that file into a DLL with the C# 3.5 compiler. Note that the C# source created by XSD has no public constructors explicitly defined.

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:

Code: Select all
public MyClass() {  }


it's generating the following:

Code: Select all
public MyClass();


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:

Code: Select all
public MyClass()
{
  base..ctor();
  return;
}


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?
The_Assimilator
 
Posts: 3
Joined: Wed Nov 05, 2008 8:58 am
Location: South Africa

Postby Clive Tong » Thu Nov 04, 2010 4:46 pm

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.

Code: Select all
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 code

Code: Select all
public 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 view

Code: Select all
  public 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 Tong
 
Posts: 283
Joined: Thu Dec 04, 2008 5:42 pm

Postby The_Assimilator » Fri Nov 05, 2010 4:46 pm

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. :)
The_Assimilator
 
Posts: 3
Joined: Wed Nov 05, 2008 8:58 am
Location: South Africa

Postby Clive Tong » Tue Nov 09, 2010 9:09 am

Thanks for the explanation... I'll add that as a feature request.
Clive Tong
 
Posts: 283
Joined: Thu Dec 04, 2008 5:42 pm


Return to .Net Reflector 6.x and .NET Reflector 6.x Pro

Who is online

Users browsing this forum: No registered users and 0 guests