Add 'Extensions.cs'
This commit is contained in:
21
Extensions.cs
Normal file
21
Extensions.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user