Results 1 to 5 of 5

Thread: What is this piece of code doing?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    330

    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;
    
                }
    
            }
    }

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,118

    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    330

    Re: What is this piece of code doing?

    Quote Originally Posted by jmcilhinney View Post
    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?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,118

    Re: What is this piece of code doing?

    Looking closer, that code is kinda weird. This part:
    vb.net Code:
    1. Class52 class52_ = new Class52(class52_);
    2. class52_.newQuery_0 = this;
    3. class52_.timer_0 = new System.Windows.Forms.Timer
    4. {
    5.     Interval = 500
    6. };
    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.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    330

    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
  •  



Click Here to Expand Forum to Full Width