-
May 29th, 2021, 08:30 PM
#1
Thread Starter
Fanatic Member
What is this piece of code doing?
I am studying one of MS Office 2019's NET library called NETQuery.dll. There're few codes I don't understand what is the function of Class52 in Reflections.
Code:
public Class52(Class52 class52_0)
Does Class52 make a reference to itself?
Code:
public sealed class NETQuery: UserControl, IMessageFilter
{
//....
[CompilerGenerated]
internal sealed class Class52
{
public System.Windows.Forms.Timer timer_0;
public NETQuery NETQuery_0;
public Class52(Class52 class52_0)
{
if (class52_0 != null)
{
timer_0 = class52_0.timer_0;
}
}
[SpecialName]
internal void method_0(object sender, EventArgs e)
{
method_1();
}
[SpecialName]
internal void method_1()
{
timer_0.Stop();
timer_0.Dispose();
timer_0 = null;
NETQuery_0.method_291(NETQuery_0, null);
}
}
//...
protected Override bool ContainerControl.ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.D | Keys.Control:
if (textEditorControl_0.ContainsFocus)
{
method_137(this, null);
return true;
}
if (gVariables.smethod_129() is Class172)
{
Class52 class52_ = new Class52(class52_);
class52_.newQuery_0 = this;
class52_.timer_0 = new System.Windows.Forms.Timer
{
Interval = 500
};
class52_.timer_0.Tick += class52_.method_0;
class52_.timer_0.Start();
return true;
}
break;
}
}
}
-
May 29th, 2021, 09:33 PM
#2
Re: What is this piece of code doing?
That is a copy constructor. It's a constructor, so it's creating a new instance, and it's taking an existing instance as an argument and copying the state of that object to the new object. In this case, that state is a single field value. It's apparently ensuring that both instances of that type are using the same Timer and will therefore act on the same schedule.
-
May 31st, 2021, 04:40 AM
#3
Thread Starter
Fanatic Member
Re: What is this piece of code doing?
Originally Posted by jmcilhinney
That is a copy constructor. It's a constructor, so it's creating a new instance, and it's taking an existing instance as an argument and copying the state of that object to the new object. In this case, that state is a single field value. It's apparently ensuring that both instances of that type are using the same Timer and will therefore act on the same schedule.
It's being marked in [CompilerGenerated], means it was generated by Compiler. Actual code is to define a normal timer and raise a Tick event, right?
-
May 31st, 2021, 04:49 AM
#4
Re: What is this piece of code doing?
Looking closer, that code is kinda weird. This part:
vb.net Code:
Class52 class52_ = new Class52(class52_);
class52_.newQuery_0 = this;
class52_.timer_0 = new System.Windows.Forms.Timer
{
Interval = 500
};
invokes that constructor to create a new instance that will use the same Timer as the existing instance, then it goes and creates a new Timer anyway. Not really sure what's going on there. Keep in mind that, if you're decompiling IL code, the result you get won't necessarily be the same as what was originally written and compiled.
-
Jun 2nd, 2021, 07:39 PM
#5
Thread Starter
Fanatic Member
Re: What is this piece of code doing?
There're many similar codes inside this dll. I extract a simple one for better understanding:
Code:
[CompilerGenerated]
internal sealed class Class150
{
public string string_0;
public Class150(Class150 class150_0)
{
if (class150_0 != null)
{
string_0 = class150_0.string_0;
}
}
internal void method_0()
{
//some task using string_0
}
}
In use:
Code:
internal static void smethod_195(bool bool_24)
{
Class150 class150_ = default(Class150);
Class150 CS_0024_003C_003E8__locals0 = new Class150(class150_);
CS_0024_003C_003E8__locals0.string_0 = (bool_24 ? "Cancel" : "Confirmed");
//...
}
Look like it is "Routing Multiple Constructors to a Single Constructor" or similar "create instances of itself inside the static constructor".
I made a simple demo:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
namespace WindowsFormsApp1
{
static class Program
{
[CompilerGenerated]
internal sealed class Class150
{
public string string_0;
public Class150(Class150 class150_0)
{
if (class150_0 != null)
{
string_0 = class150_0.string_0;
}
}
internal void method_0()
{
//some task using string_0
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Class150 class150_ = default(Class150);
Class150 CS_0024_003C_003E8__locals0 = new Class150(class150_);
CS_0024_003C_003E8__locals0.string_0 = "Cancel";
Class150 CS_0024_003C_003E8__locals1 = new Class150(CS_0024_003C_003E8__locals0);
CS_0024_003C_003E8__locals0.string_0 = "Confirmed";
}
}
}
It works, but I want to know what is the original code before reflection.
Last edited by DaveDavis; Jun 2nd, 2021 at 08:03 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|