0

In .net decomplilers, i'm not able to see the code for constructors. I've tested in many .net decompilers but none of them are showing constructor code. Is there any decomplier which shows that code.

Thanks in advance enter image description here

Shekar Reddy
  • 616
  • 7
  • 18
  • Show C# and relevant MSIL for a small example that reproduces your problem. Are you sure there is supposed to be a constructor, and what would that look like? – CodeCaster Oct 09 '13 at 10:59
  • I can't look at .NET Reflector now, but did you find a _ctor() method, which would be the constructors equivalent? – Ray Oct 09 '13 at 10:59
  • I'm trying to decomplile the Microsoft.Sharepoint.WebControls.MultipleLookupField there are two constructors in this class, and I can't see the _ctor() too – Shekar Reddy Oct 09 '13 at 11:02
  • 5
    The `public MultipleLookupField()` method is the constructor of `class MultipleLookupField`. – dtb Oct 09 '13 at 11:13
  • Ho.. yeah, but it is not showing source code. just signature :-( – Shekar Reddy Oct 09 '13 at 11:15
  • 2
    Likely there is no constructor code worth decompiling. Also, the image you've posted show no code at all, which means you've probably not asked the decompiler to do that. – Lasse V. Karlsen Oct 09 '13 at 11:37
  • My experience with JetBrains dotPeek is that it normally shows IL as pseudo-comments in the main window for the file, but to see the IL for a constructor you need to open the IL Viewer, which is under the Windows menu item. – RenniePet Feb 22 '18 at 04:45

2 Answers2

4

All .NET types defined by C# (I don't know how VB.NET or other languages do this, they might do the same, they might not) will have a constructor.

If you don't explicitly add a constructor, one will be provided for you by the compiler.

I've tested (earlier) the Telerik decompiler, Reflector, and dotPeek by JetBrains, and they all show constructor code when there is code to show.

Here's dotPeek 1.1 showing the constructor of Tuple<T1, T2>:

Tuple<T1, T2> constructor decompiled by dotPeek 1.1

However, the decompiler might opt to remove the constructor because it cannot see your original source code, so instead it tries to infer what kind of code would generate the IL you gave it. It is thus likely that even if your code explicitly writes out an empty constructor, it is compiled 100% identical to what the compiler would provide for you without it, and the decompiler thus assumes you didn't write out an empty constructor.

TL;DR: Judging by the documentation of the MultipleLookupField constructor, it has no parameters. It is thus possible that its only purpose is to call the base constructor and nothing else. As such, it may be completely unnecessary to decompile since a likely case was that one was not written out in the source code either.

However, to show that all classes have constructors, let's look at some examples.

The following examples can be tested in LINQPad by executing the code and then clicking the IL button above the results to see the decompiled IL. The decompiler of LINQPad (mono.cecil I belive) does not do this kind of inference to the same level so it will show the constructors.

Example 1: No constructor or code that would be placed in the constructor

void Main()
{
    var t = new Test();
    t.Execute();
}

public class Test
{
    public void Execute()
    {
        Debug.WriteLine("Execute");
    }
}

Generated IL:

IL_0000:  newobj      UserQuery+Test..ctor
IL_0005:  stloc.0     // t
IL_0006:  ldloc.0     // t
IL_0007:  callvirt    UserQuery+Test.Execute

Test.Execute:
IL_0000:  ldstr       "Execute"
IL_0005:  call        System.Diagnostics.Debug.WriteLine
IL_000A:  ret         

Test..ctor:
IL_0000:  ldarg.0     
IL_0001:  call        System.Object..ctor
IL_0006:  ret     

As you can see, there is a constructor in there, but all it does is call the base constructor from System.Object.

Example 2: Adding a string field with a default value

Now let's do something else, add this field to the class:

public class Test
{
    private string _Value = string.Empty;

    public void Execute()
    {
        Debug.WriteLine("Execute");
    }
}

Re-run and here's the generated IL:

IL_0000:  newobj      UserQuery+Test..ctor
IL_0005:  stloc.0     // t
IL_0006:  ldloc.0     // t
IL_0007:  callvirt    UserQuery+Test.Execute

Test.Execute:
IL_0000:  ldstr       "Execute"
IL_0005:  call        System.Diagnostics.Debug.WriteLine
IL_000A:  ret         

Test..ctor:
IL_0000:  ldarg.0     
IL_0001:  ldsfld      System.String.Empty
IL_0006:  stfld       UserQuery+Test._Value
IL_000B:  ldarg.0     
IL_000C:  call        System.Object..ctor
IL_0011:  ret 

As you can see, the initialization code was "lifted" into the constructor, thus the constructor now initializes the field.

Example 3: Moving the initialization into the constructor

Let's change the class to this:

public class Test
{
    private string _Value;

    public Test()
    {
        _Value = string.Empty;
    }

    public void Execute()
    {
        Debug.WriteLine("Execute");
    }
}

Here's the IL of the constructor now:

Test..ctor:
IL_0000:  ldarg.0     
IL_0001:  call        System.Object..ctor
IL_0006:  ldarg.0     
IL_0007:  ldsfld      System.String.Empty
IL_000C:  stfld       UserQuery+Test._Value
IL_0011:  ret 

The difference here now is the ordering of the code in the constructor, the field is initialized after the base constructor was called, whereas it was initialized before in the previous example.

Conclusion

There might be no constructor in those types, or it might be that those decompilers doesn't know the difference between initializing the fields in their declarations vs. in the constructor. I find the latter unlikely however, so I would assume that the class has no constructor.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
1

Telerik JustDecompile shows constructor code.

Erwin
  • 3,060
  • 26
  • 24
  • All the decompilers show constructor code. The likely case, however, is that the type(s) in question does not in fact have a constructor worth decompiling, as it is identical to what the compiler would provide for you if you don't write it manually. – Lasse V. Karlsen Oct 09 '13 at 11:36
  • I noticed now that his screenshot shows no code at all, only declarations/signatures. I think it is much more likely that he hasn't asked Reflector to decompile, only to show top-level declarations and signatures. – Lasse V. Karlsen Oct 09 '13 at 11:43
  • Ho.. yes.. my bad. I was checking the wrong class which does not has constructor ... Thank you very very much for the help.. – Shekar Reddy Oct 09 '13 at 11:52