public static bool SetProperty(object source, string property, object value, bool checkCollecionsProperties = false, bool invoked = false) { Type TSource = source.GetType(); bool bReturn = false; if (invoked && source is Control) { ((Control)source).Invoke((MethodInvoker)(() => { PropertyInfo PInfo = TSource.GetProperty(property); if (PInfo != null) { PInfo.SetValue(source, value, null); bReturn = true; } else { bReturn = false; } })); } else { return(Objects.SetProperty(source, property, value, true)); } return(bReturn); }
/// <summary> /// This method allows to set multiple properties into source object by its names /// </summary> /// <param name="destination"></param> /// <param name="source"></param> /// <param name="canRead"></param> /// <returns></returns> public static bool SetProperties <TDestination>(ref TDestination destination, Dictionary <string, object> source, ThreeState canRead) { bool success = true; foreach (KeyValuePair <string, object> KVP in source) { if (!Objects.SetProperty <TDestination>(ref destination, KVP.Key, KVP.Value, canRead)) { success = false; } } return(success); }
public static void InjectDictionary(Dictionary <object, object> source, object destination, bool invoked = false) { foreach (var v in source) { object obj = Objects.GetProperty(destination, v.Key.ToString(), true, invoked); if (obj != null) { if (v.Value is IDictionary) { InjectDictionary((Dictionary <object, object>)v.Value, obj, invoked); } else { Objects.SetProperty(obj, v.Key.ToString(), v.Value, false, invoked); } } else if (destination is ICollection && destination is IEnumerable) { var vOEnum = (destination as IEnumerable); foreach (object o in vOEnum) { string sNameof = Objects.nameof(o); if (sNameof != v.Key.ToString()) { continue; } if (v.Value is IDictionary) { Dictionary <object, object> DO2VVal = (Dictionary <object, object>)v.Value; foreach (var v2 in DO2VVal) { if (DO2VVal.ContainsKey(v2.Key + sNodeKeyID)) { Objects.SetProperty(o, v2.Key.ToString(), v2.Value, false, invoked); } else if (v2.Value is IDictionary) { InjectDictionary((Dictionary <object, object>)v2.Value, o, invoked); } } } } } } }