Add Reflections.cs
This commit is contained in:
75
Reflections.cs
Normal file
75
Reflections.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copying Object Properties to Dictionary
|
||||
public static Dictionary<string, object> ToDictionary(this object obj) {
|
||||
Dictionary<string, object> r = new Dictionary<string, object>();
|
||||
PropertyInfo inf = null;
|
||||
Type t = obj.GetType();
|
||||
try {
|
||||
foreach (PropertyInfo i in t.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
|
||||
inf = i;
|
||||
try {
|
||||
r.Add(inf.Name, inf.GetValue(obj));
|
||||
} catch (Exception ex) { Log.Error(ex, $"Object.ToDictionary.GetValue[{inf.Name}] failed"); }
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Log.Error(ex, $"Unable to PopulateControls with Object [Type={t.ToString()}, PropertyInfo.Name={inf.Name}]");
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Copying Object Properties to Another Object
|
||||
* This method will copy properties with the same name and same value type.
|
||||
*/
|
||||
public static t Parse<t>(this object obj) {
|
||||
var r = typeof(t).GetConstructor(new System.Type[] { }).Invoke(new object[] { });
|
||||
PropertyInfo inf = null;
|
||||
try {
|
||||
foreach (PropertyInfo i in r.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
|
||||
inf = i;
|
||||
try {
|
||||
PropertyInfo inf2 = obj.GetType().GetProperty(inf.Name);
|
||||
if (inf2!= null) {
|
||||
inf.SetValue(r, inf2.GetValue(obj));
|
||||
}
|
||||
} catch (Exception ex) { Log.Error(ex, $"Object.Parse.GetValue[{inf.Name}] failed"); }
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Log.Error(ex, $"Unable to PopulateControls with Object [Type={obj.GetType().ToString()}, PropertyInfo.Name={inf.Name}]");
|
||||
}
|
||||
return (t)r;
|
||||
}
|
||||
|
||||
/*
|
||||
* Copying Event Handlers from One Object to Another
|
||||
* This method will copy the event handlers assigned to one object, to another object.
|
||||
* This information was obtained via: https://stackoverflow.com/questions/293007/is-it-possible-to-steal-an-event-handler-from-one-control-and-give-it-to-anoth
|
||||
*/
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Forms;
|
||||
using System.Reflection;
|
||||
|
||||
namespace WindowsFormsApplication1 {
|
||||
public partial class Form1: Form {
|
||||
public Form1() {
|
||||
InitializeComponent();
|
||||
button1.Click += new EventHandler(button1_Click);
|
||||
// Get secret click event key
|
||||
FieldInfo eventClick = typeof(Control).GetField("EventClick", BindingFlags.NonPublic | BindingFlags.Static);
|
||||
object secret = eventClick.GetValue(null);
|
||||
// Retrieve the click event
|
||||
PropertyInfo eventsProp = typeof(Component).GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
EventHandlerList events = (EventHandlerList)eventsProp.GetValue(button1, null);
|
||||
Delegate click = events[secret];
|
||||
// Remove it from button1, add it to button2
|
||||
events.RemoveHandler(secret, click);
|
||||
events = (EventHandlerList)eventsProp.GetValue(button2, null);
|
||||
events.AddHandler(secret, click);
|
||||
}
|
||||
|
||||
void button1_Click(object sender, EventArgs e) {
|
||||
MessageBox.Show("Yada");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user