/// <summary> /// Function for List initializer /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Add(string key, object value) { if (IdentifierExtensions.IsValidIdentifier(key) == false) { throw new ArgumentException($"{key} is not a valid c# identifier"); } internalDictionary.Add(key, DataObjectFactory.GetPropertyObjectFor(key, value)); RaiseCollectionChanged(NotifyCollectionChangedAction.Add, internalDictionary[key]); }
/// <summary> /// Generic method to add <see cref="DataObject"/> based on <see cref="Type"/> of <paramref name="value"/> /// </summary> /// <param name="name"></param> /// <param name="value"></param> /// <returns></returns> public DataContainerBuilder Data(string name, object value) { if (config.ContainsData(name) || value is null) { DataContainerEvents.NotifyInformation($"Attempted to add invalid value : {name}"); return(this); } config.Add(DataObjectFactory.GetDataObjectFor(name, value)); return(this); }
/// <summary> /// Generic method to create all types of objects /// Addes a different implementation of <see cref="PropertyObject"/> based on <see cref="Type"/> /// of <paramref name="value"/> /// </summary> /// <param name="name">name of property to be added</param> /// <param name="value">value of property</param> /// <param name="propertyBuilder">builder to set additional properties of <see cref="PropertyObject"/></param> /// <returns></returns> public PropertyContainerBuilder Property(string name, object value, Action <PropertyObjectBuilder> propertyBuilder = null) { if (config.ContainsData(name) || value is null) { DataContainerEvents.NotifyInformation($"Attempted to add invalid value : {name}"); return(this); } if (DataObjectFactory.GetPropertyObjectFor(name, value) is PropertyObject obj) { propertyBuilder?.Invoke(new PropertyObjectBuilder(obj)); config.Add(obj); } return(this); }
public static void PutValue(this IDataContainer container, string key, object value) { if (container.ContainsData(key)) { container.SetValue(key, value); } else { if (container is IPropertyContainer) { container.Add(DataObjectFactory.GetPropertyObjectFor(key, value)); } else { container.Add(DataObjectFactory.GetDataObjectFor(key, value)); } } }
/// <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> /// Implementation for <see cref="DataObject.ReadXmlElement(string, XmlReader)"/> /// </summary> /// <param name="elementName"></param> /// <param name="reader"></param> /// <returns></returns> protected override bool ReadXmlElement(string elementName, XmlReader reader) { // call base if (base.ReadXmlElement(elementName, reader)) { return(true); } /// Read DataObject implementation if (elementName == ContainerDataObject.DC_START_ELEMENT_NAME) { var obj = DataObjectFactory.GetDataObject("dc"); if (obj != null) { using (var newReader = XmlReader.Create(new StringReader(reader.ReadOuterXml()))) { newReader.Read(); obj.ReadXml(newReader); if (obj.Type != DataObjectType.NotSupported) { readingHelper.Add(obj); } } } return(true); } /// Read type info else if (elementName == nameof(TypeInfo)) { CollectionType = reader.ReadObjectXml <TypeInfo>(); return(true); } return(false); }
/// <summary> /// Registers type converter for <see cref="DataObject"/> with <see cref="DataObject.Type"/> equal to <paramref name="typeid"/> /// this type will be use as parameter for <see cref="TypeConverterAttribute"/> which is used by WindowsForms PropertyGrid /// </summary> /// <typeparam name="TEditor"></typeparam> /// <param name="typeid"></param> public static void RegisterConverter <TConverter>(string typeid) where TConverter : TypeConverter { var dataobj = DataObjectFactory.GetPropertyObject(typeid); if (dataobj is null) { return; } var type = dataobj.GetType(); if (converterMapping.ContainsKey(type)) { converterMapping[type] = typeof(TConverter); } else { converterMapping.Add(type, typeof(TConverter)); } }
/// <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); }
/// <summary> /// Implementation for <see cref="DataObject.ReadXmlElement(string, XmlReader)"/> /// </summary> /// <param name="elementName"></param> /// <param name="reader"></param> /// <returns></returns> protected override bool ReadXmlElement(string elementName, XmlReader reader) { // call base implementation if (base.ReadXmlElement(elementName, reader)) { return(true); } /// Read underlying type for <see cref="ContainerDataObject"/> created from CLR objects if (elementName == nameof(TypeInfo)) { ObjectType = reader.ReadObjectXml <TypeInfo>(); return(true); } /// Read <see cref="DataObject"/> implementation else { if (DataObjectFactory.GetPropertyObject(reader.GetAttribute(TYPE_ID_ATTRIBUTE)) is DataObject obj) { using (var newReader = XmlReader.Create(new StringReader(reader.ReadOuterXml()))) { newReader.Read(); obj.ReadXml(newReader); if (obj.Type != DataObjectType.NotSupported) { _container.Add(obj); } } return(true); } } return(false); }
/// <summary> /// Method used internally to create <see cref="IPropertyContainer"/> from CLR objects /// </summary> /// <param name="pi"></param> /// <param name="obj"></param> /// <returns></returns> private PropertyContainerBuilder Property(PropertyInfo pi, object obj) { if (pi.GetValue(obj) is object value) { var option = pi.GetBrowseOption(); var description = pi.GetDescription(); var category = pi.GetCategory(); var displayName = pi.GetDisplayName(); // Why is this here ?? if (value is IDataContainer dc && dc.Count == 0) { return(this); } config.Add(DataObjectFactory.GetPropertyObjectFor(pi.Name, value)? .SetBrowsePermission(option) .SetDescription(description) .SetCategory(category) .SetDisplayName(displayName)); } return(this); }
/// <summary> /// Implementation for <see cref="DataContainerBase.GetUnitializedDataObject(string)"/> /// </summary> /// <param name="type"></param> /// <returns></returns> protected override DataObject GetUnitializedDataObject(string type) { return(DataObjectFactory.GetPropertyObject(type)); }
/// <summary> /// Get an in unintialized <see cref="DataObject"/> based on <paramref name="type"/> /// values will be populated from xml when <see cref="DataObject.ReadXml(XmlReader)"/> is called /// </summary> /// <param name="type"></param> /// <returns></returns> protected virtual DataObject GetUnitializedDataObject(string type) { return(DataObjectFactory.GetDataObject(type)); }