How to Decompile .NET DLL to Source Code using Reflector

By FoxLearn 9/16/2024 9:48:07 AM   10.55K
To decompile a .NET DLL to C# source code using Reflector, you can follow these steps.

In this example, i'm using Reflector to decompile Xander.dll to C# Source Code.

You can view this post: Xander UI Framework WinForm App in C# to know How to download and install xander.dll framework.

Xander UI is a library that helps you design an awesome UI for Windows Forms Application.

c# xander ui

To play the demo, you should download the XanderUI.dll

Next, you can download .NET Reflector to help you decompile .net into source code.

Reflector is a powerful tool for decompiling .NET assemblies, including DLLs, into readable C# or VB.NET code. Here's a step-by-step guide on how to decompile a .NET DLL to C# source code using Reflector:

Once installed, launch Reflector on your computer.

In Reflector, go to File > Open, and then navigate to the location of the DLL file you want to decompile. Select the DLL file and click "Open".

If you want to save the decompiled code to a file, you can do so by selecting the desired code in Reflector and then choosing File > Save As. Select a location on your computer and provide a file name for the exported code.

Create a UICheckBox class, then copy the CheckBox source code of the Xander library that you've decompiled.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Forms;

namespace XanderUI
{
    public class UICheckBox : Control
    {
        // Fields
        private Color checkboxCheckColor;
        private Color checkboxColor;
        private Color checkboxHoverColor;
        private Style checkboxStyle;
        [CompilerGenerated]
        private EventHandler checkedStateChanged;
        private Color currentColor;
        private bool isChecked;
        private int tickThickness;

        // Events
        public event EventHandler CheckedStateChanged
        {
            [CompilerGenerated]
            add
            {
                EventHandler handler2;
                EventHandler checkedStateChanged = this.checkedStateChanged;
                do
                {
                    handler2 = checkedStateChanged;
                    EventHandler handler3 = (EventHandler)Delegate.Combine(handler2, value);
                    checkedStateChanged = Interlocked.CompareExchange<EventHandler>(ref this.checkedStateChanged, handler3, handler2);
                }
                while (checkedStateChanged != handler2);
            }
            [CompilerGenerated]
            remove
            {
                EventHandler handler2;
                EventHandler checkedStateChanged = this.checkedStateChanged;
                do
                {
                    handler2 = checkedStateChanged;
                    EventHandler handler3 = (EventHandler)Delegate.Remove(handler2, value);
                    checkedStateChanged = Interlocked.CompareExchange<EventHandler>(ref this.checkedStateChanged, handler3, handler2);
                }
                while (checkedStateChanged != handler2);
            }
        }


        // Methods
        public UICheckBox()
        {
            this.tickThickness = 2;
            this.checkboxColor = Color.FromArgb(0, 0xa2, 250);
            this.checkboxCheckColor = Color.White;
            this.checkboxHoverColor = Color.FromArgb(0xf9, 0x37, 0x62);
            this.checkboxStyle = Style.Material;
            base.Size = new Size(100, 20);
            this.Text = base.Name;
            this.ForeColor = Color.White;
            this.currentColor = this.checkboxColor;
        }

        protected virtual void OnCheckedStateChanged()
        {
            if (this.checkedStateChanged != null)
            {
                this.checkedStateChanged(this, new EventArgs());
            }
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            if (!this.Checked)
            {
                this.Checked = true;
            }
            else
            {
                this.Checked = false;
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.currentColor = this.checkboxHoverColor;
            base.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.currentColor = this.checkboxColor;
            base.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            if (this.checkboxStyle == Style.Material)
            {
                e.Graphics.FillRectangle(new SolidBrush(this.currentColor), 1, 1, base.Height - 2, base.Height - 2);
                if (this.isChecked)
                {
                    e.Graphics.DrawLine(new Pen(this.checkboxCheckColor, (float)this.tickThickness), 2, (base.Height / 3) * 2, base.Height / 2, base.Height - 2);
                    e.Graphics.DrawLine(new Pen(this.checkboxCheckColor, (float)this.tickThickness), base.Height / 2, base.Height - 2, base.Height - 2, 1);
                }
            }
            if (this.checkboxStyle == Style.iOS)
            {
                if (!this.isChecked)
                {
                    e.Graphics.DrawEllipse(new Pen(Color.FromArgb(200, 200, 200)), 2, 2, base.Height - 4, base.Height - 4);
                }
                if (this.isChecked)
                {
                    e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(0, 120, 0xff)), 1, 1, base.Height - 2, base.Height - 2);
                    e.Graphics.DrawLine(new Pen(Color.White, (float)this.tickThickness), (int)(base.Height / 5), (int)(base.Height / 2), (int)(base.Height / 2), (int)((base.Height / 4) * 3));
                    e.Graphics.DrawLine(new Pen(Color.White, (float)this.tickThickness), (int)(base.Height / 2), (int)((base.Height / 4) * 3), (int)((base.Height / 5) * 4), (int)(base.Height / 4));
                }
            }
            StringFormat format = new StringFormat();
            format.LineAlignment = StringAlignment.Center;
            format.Alignment = StringAlignment.Near;
            SolidBrush brush = new SolidBrush(this.ForeColor);
            RectangleF layoutRectangle = new RectangleF((float)(base.Height + 3), 0f, (float)((base.Width - base.Height) - 2), (float)base.Height);
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            e.Graphics.DrawString(this.Text, this.Font, brush, layoutRectangle, format);
            base.OnPaint(e);
        }

        // Properties
        [Category("XanderUI"), Browsable(true), Description("Checkbox color")]
        public Color CheckboxCheckColor
        {
            get
            {
                return this.checkboxCheckColor;
            }
            set
            {
                this.checkboxCheckColor = value;
                base.Invalidate();
            }
        }

        [Category("XanderUI"), Browsable(true), Description("Checkbox color")]
        public Color CheckboxColor
        {
            get
            {
                return this.checkboxColor;
            }
            set
            {
                this.checkboxColor = value;
                this.currentColor = value;
                base.Invalidate();
            }
        }

        [Category("XanderUI"), Browsable(true), Description("Checkbox color when hovering")]
        public Color CheckboxHoverColor
        {
            get
            {
                return this.checkboxHoverColor;
            }
            set
            {
                this.checkboxHoverColor = value;
                base.Invalidate();
            }
        }

        [Category("XanderUI"), Browsable(true), Description("The Checkbox style")]
        public Style CheckboxStyle
        {
            get
            {
                return this.checkboxStyle;
            }
            set
            {
                this.checkboxStyle = value;
                base.Invalidate();
            }
        }

        [Category("XanderUI"), Browsable(true), Description("Checked or unchecked")]
        public bool Checked
        {
            get
            {
                return this.isChecked;
            }
            set
            {
                this.isChecked = value;
                this.OnCheckedStateChanged();
                base.Invalidate();
            }
        }

        [Category("XanderUI"), Browsable(true), Description("Thickness of the tick when checked")]
        public int TickThickness
        {
            get
            {
                return this.tickThickness;
            }
            set
            {
                this.tickThickness = value;
                base.Invalidate();
            }
        }

        // Nested Types
        public enum Style
        {
            iOS,
            Material
        }
    }
}

Rebuild your project, then you can see the UICheckBox in your visual studio toolbox.

Finally, Open your form designer, then drag the UICheckBox from the visual studio toolbox to your winform.

VIDEO TUTORIAL