/// <summary> /// Gets Data Value from a DataContainer object /// </summary> /// <typeparam name="T">Type of result</typeparam> /// <param name="key">Key, which uniquely identifies the data</param> /// <param name="value">Value object passed as reference</param> /// <returns>Is Success</returns> public virtual bool GetValue <T>(string key, ref T value) { var data = this.FindRecursive(key); bool result = false; if (data != null && data.GetValue() is T val) { value = val; result = true; } else { DataContainerEvents.NotifyError($"Unable to find \"{key}\""); } return(result); }
/// <summary> /// Generic method for adding numeric types /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <param name="value"></param> /// <param name="propertyBuilder"></param> /// <returns></returns> public PropertyContainerBuilder Number <T>(string name, T value, Action <NumericPropertyObjectBuilder <T> > propertyBuilder = null) where T : struct, IComparable, IComparable <T>, IConvertible, IEquatable <T>, IFormattable { if (config.ContainsData(name)) { DataContainerEvents.NotifyInformation($"Attempted to add invalid value : {name}"); return(this); } if (DataObjectFactory.GetPropertyObjectFor(name, value) is PropertyObject obj) { propertyBuilder?.Invoke(new NumericPropertyObjectBuilder <T>(obj)); config.Add(obj); } return(this); }
/// <summary> /// Load state from binary file /// </summary> /// <param name="path"></param> /// <returns></returns> public static IPropertyContainer FromBinaryFile(string path) { IFormatter formatter = new BinaryFormatter { Binder = new CustomizedBinder() }; try { using (var stream = new FileStream(path, FileMode.Open)) { return((IPropertyContainer)formatter.Deserialize(stream)); } } catch (Exception ex) { DataContainerEvents.NotifyError($"Error reading file :{path}, {ex}"); return(null); } }
/// <summary> /// Internal method to create <see cref="IDataContainer"/> from CLR Objects /// </summary> /// <param name="pi"></param> /// <param name="obj"></param> /// <returns></returns> private DataContainerBuilder Data(PropertyInfo pi, object obj) { var value = pi.GetValue(obj); if (value is null) { DataContainerEvents.NotifyInformation($"Attempted to add invalid value : {pi.Name}"); return(this); } // ?? if (value is IDataContainer dc && dc.Count == 0) { return(this); } config.Add(DataObjectFactory.GetDataObjectFor(pi.Name, value)); return(this); }