From 45dea047be95cfbc7131331ae1e1c1d9528df149 Mon Sep 17 00:00:00 2001 From: Russ Kollmansberger Date: Fri, 19 Jul 2024 08:42:53 -0500 Subject: [PATCH] Add Reflections.cs --- Reflections.cs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Reflections.cs diff --git a/Reflections.cs b/Reflections.cs new file mode 100644 index 0000000..585dd57 --- /dev/null +++ b/Reflections.cs @@ -0,0 +1,75 @@ +// Copying Object Properties to Dictionary +public static Dictionary ToDictionary(this object obj) { + Dictionary r = new Dictionary(); + 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(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"); + } + } +} \ No newline at end of file