Red Gate forums :: View topic - Bug: implicit no-arg constructors generated incorrectly?
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
.Net Reflector 6.x and .NET Reflector 6.x Pro
.Net Reflector 6.x and .NET Reflector 6.x Pro forum

Bug: implicit no-arg constructors generated incorrectly?

Search in .Net Reflector 6.x and .NET Reflector 6.x Pro forum
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.
Jump to:  
Author Message
The_Assimilator



Joined: 05 Nov 2008
Posts: 3
Location: South Africa

PostPosted: Wed Nov 03, 2010 3:39 pm    Post subject: Bug: implicit no-arg constructors generated incorrectly? Reply with quote

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:

public MyClass() {  }


it's generating the following:

Code:

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:

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?
Back to top
View user's profile Send private message
Clive Tong



Joined: 04 Dec 2008
Posts: 283

PostPosted: Thu Nov 04, 2010 4:46 pm    Post subject: Reply with quote

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:

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:

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:

  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#.
Back to top
View user's profile Send private message
The_Assimilator



Joined: 05 Nov 2008
Posts: 3
Location: South Africa

PostPosted: Fri Nov 05, 2010 4:46 pm    Post subject: Reply with quote

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. Smile
Back to top
View user's profile Send private message
Clive Tong



Joined: 04 Dec 2008
Posts: 283

PostPosted: Tue Nov 09, 2010 9:09 am    Post subject: Reply with quote

Thanks for the explanation... I'll add that as a feature request.
Back to top
View user's profile Send private message
Display posts from previous:   
This topic is locked: you cannot edit posts or make replies. All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group