Reflection load all classes properties constructors (edit)

Generate class DTO from Assembly

manhng83/reflectionexample (github.com)

UI

namespace WindowsFormsApp1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.label4 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(238, 482);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(139, 59);
this.button1.TabIndex = 0;
this.button1.Text = "Load Properties";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(238, 48);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(470, 22);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "PERSON";
//
// button2
//
this.button2.Location = new System.Drawing.Point(727, 482);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(111, 59);
this.button2.TabIndex = 2;
this.button2.Text = "Exit";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(238, 76);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(600, 182);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "";
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.SystemColors.ControlDark;
this.textBox2.Location = new System.Drawing.Point(238, 20);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(600, 22);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "C:\\Git\\Domain.dll";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(127, 23);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(105, 17);
this.label1.TabIndex = 5;
this.label1.Text = "Assembly Path:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(145, 51);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(87, 17);
this.label2.TabIndex = 6;
this.label2.Text = "Class Name:";
//
// button3
//
this.button3.Location = new System.Drawing.Point(390, 482);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(139, 59);
this.button3.TabIndex = 7;
this.button3.Text = "Copy Result";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(180, 76);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(52, 17);
this.label3.TabIndex = 8;
this.label3.Text = "Result:";
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(238, 264);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(600, 163);
this.richTextBox2.TabIndex = 9;
this.richTextBox2.Text = "IDCOUVERTURE\nIDPATTERN";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(74, 267);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(158, 17);
this.label4.TabIndex = 10;
this.label4.Text = "Only include properties:";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(238, 447);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(114, 21);
this.checkBox1.TabIndex = 11;
this.checkBox1.Text = "All Properties";
this.checkBox1.UseVisualStyleBackColor = true;
//
// button4
//
this.button4.Location = new System.Drawing.Point(542, 482);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(172, 59);
this.button4.TabIndex = 12;
this.button4.Text = "Classes && Properties";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button5.Location = new System.Drawing.Point(714, 44);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(124, 29);
this.button5.TabIndex = 13;
this.button5.Text = "Load Classes";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(870, 553);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.label4);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.label3);
this.Controls.Add(this.button3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Reflection";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
}
}

Code

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private HashSet<string> properties = new HashSet<string>();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
}

private void button1_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;

string className = textBox1.Text.Trim();

string pathToDLL = textBox2.Text.Trim();

Assembly assembly = Assembly.LoadFile(pathToDLL);

string nameSpace = assembly.GetName().Name;

Type aType = assembly.GetType($"{nameSpace}.{className}");

var sb = new StringBuilder();

foreach (var property in aType.GetProperties())
{
var name = property.Name;
var propertyType = property.PropertyType;
if (propertyType.IsGenericType &&
propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = propertyType.GetGenericArguments()[0];
var type = GetCSharpType(propertyType.FullName);
if (type == "object") continue;
sb.AppendLine($"public {type}? {name}" + " { get; set; }");
properties.Add(name);
}
else
{
var type = GetCSharpType(propertyType.FullName);
if (type == "object") continue;
sb.AppendLine($"public {type} {name}" + " { get; set; }");
properties.Add(name);
}
}

richTextBox1.Text = sb.ToString();

Clipboard.SetText(sb.ToString());

MessageBox.Show("OK");

Cursor.Current = Cursors.Default;
}

private void button3_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;

var arr = (from x in richTextBox2.Text.Split(new string[] { "\n", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) select x.Trim()).ToList();

var sb = new StringBuilder();
string className = textBox1.Text.Trim();
string classNameDTO = textBox1.Text.Trim() + "DTO";
sb.AppendLine($"public class {classNameDTO}");
sb.AppendLine("{");

sb.AppendLine(richTextBox1.Text);

sb.AppendLine($"public {classNameDTO}()");
sb.AppendLine("{");
sb.AppendLine("}");

sb.AppendLine($"public {classNameDTO}({className} obj{className})");
sb.AppendLine("{");

var list = properties.ToList<string>();

if (checkBox1.Checked)
{
for (int i = 0; i < list.Count; i++)
{
sb.AppendLine($"{list[i]} = obj{className}.{list[i]};");
}
}
else
{
for (int i = 0; i < arr.Count; i++)
{
sb.AppendLine($"{arr[i]} = obj{className}.{arr[i]};");
}
}

sb.AppendLine("}");

sb.AppendLine("}");

Clipboard.SetText(sb.ToString());

MessageBox.Show("OK");

Cursor.Current = Cursors.Default;
}

private void button5_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;

string pathToDLL = textBox2.Text.Trim();

Assembly mscorlib = Assembly.LoadFile(pathToDLL);

var lst = new List<string>();

var sb = new StringBuilder();

foreach (Type type in mscorlib.GetTypes())
{
lst.Add(type.FullName);
sb.AppendLine(type.FullName);
}

richTextBox1.Text = sb.ToString();

Clipboard.SetText(sb.ToString());

MessageBox.Show("OK");

Cursor.Current = Cursors.Default;
}

private void button4_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;

string pathToDLL = textBox2.Text.Trim();

Assembly mscorlib = Assembly.LoadFile(pathToDLL);

var sb = new StringBuilder();

foreach (Type aType in mscorlib.GetTypes())
{
sb.AppendLine();

string className = aType.Name;

sb.AppendLine($"public class {className}");
sb.AppendLine("{");

foreach (var property in aType.GetProperties())
{
var name = property.Name;
var propertyType = property.PropertyType;
if (propertyType.IsGenericType &&
propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = propertyType.GetGenericArguments()[0];
var type = GetCSharpType(propertyType.FullName);
if (type == "object") continue;
sb.AppendLine($" public {type}? {name}" + " { get; set; }");
properties.Add(name);
}
else
{
var type = GetCSharpType(propertyType.FullName);
if (type == "object") continue;
sb.AppendLine($" public {type} {name}" + " { get; set; }");
properties.Add(name);
}
}

sb.AppendLine($" public {className}()");
sb.AppendLine(" {");
sb.AppendLine(" }");

sb.AppendLine("}");
}

richTextBox1.Text = sb.ToString();

Clipboard.SetText(sb.ToString());

MessageBox.Show("OK");

Cursor.Current = Cursors.Default;
}

private void button2_Click(object sender, EventArgs e)
{
Environment.Exit(-1);
}

private string GetCSharpType(string fullName)
{
string type = "object";

switch (fullName)
{
case "System.Object":
type = "object";
break;

case "System.String":
type = "string";
break;

case "System.Boolean":
type = "bool";
break;

case "System.Byte":
type = "byte";
break;

case "System.SByte":
type = "sbyte";
break;

case "System.Int16":
type = "short";
break;

case "System.UInt16":
type = "ushort";
break;

case "System.Int32":
type = "int";
break;

case "System.UInt32":
type = "uint";
break;

case "System.Int64":
type = "long";
break;

case "System.UInt64":
type = "ulong";
break;

case "System.Single":
type = "float";
break;

case "System.Double":
type = "double";
break;

case "System.Decimal":
type = "decimal";
break;

case "System.Char":
type = "char";
break;

case "System.DateTime":
type = "DateTime";
break;

default:
break;
}

return type;
}
}
}