From c431fc3dd6594d16d51a8368813bf746c1c46301 Mon Sep 17 00:00:00 2001 From: rlkollman Date: Sun, 30 Jan 2022 14:50:46 -0600 Subject: [PATCH] Add 'Extensions.cs' --- Extensions.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Extensions.cs diff --git a/Extensions.cs b/Extensions.cs new file mode 100644 index 0000000..fb8f3c2 --- /dev/null +++ b/Extensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Reflection; + +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; +} \ No newline at end of file