public AdapterType(string name, string assemblyAndClrType, string description, ClassMetadata configurationMetadata) { Name = name; AssemblyAndClrType = assemblyAndClrType; Description = description; ConfigurationMetadata = configurationMetadata; }
public AdapterType(string name, string assemblyAndClrType, string description, Type clrType) { Name = name; AssemblyAndClrType = assemblyAndClrType; Description = description; ConfigurationMetadata = new ClassMetadata(clrType); }
internal ConfigurationEditor(ClassMetadata classMetadata) { InitializeComponent(); ClassMetadata = classMetadata; PrepareItemEditors(); }
private ItemMetadata(ClassMetadata owner, ItemMemberKind memberKind, Type clrType, string name, ConfigurationItemAttribute attribute, IEnumerable<ValueValidatorAttribute> validators) { Owner = owner; MemberKind = memberKind; Name = name; Type = ItemType.GetItemType(clrType); UserDescription = attribute.UserDescription; Validators = new ReadOnlyCollection<ValueValidatorAttribute>(validators.ToArray()); if (attribute.UserName != null) { UserName = attribute.UserName; } else { UserName = Name; } VerifyDefaultValue(clrType, attribute.DefaultValue, Name); SerializedDefaultValue = new XElement(XName.Get("defaultValue")); #region In case of dictionary type, convert default array of keys and values into dictionary if ((Type is DictionaryItemType) && (attribute.DefaultValue != null)) { Dictionary<object, object> defaultDictionary = new Dictionary<object, object>(); Array defaultArray = (Array)attribute.DefaultValue; for (int i = 0; i < defaultArray.Length / 2; i++) { object key = defaultArray.GetValue((i * 2) + 0); object value = defaultArray.GetValue((i * 2) + 1); defaultDictionary.Add(key, value); } attribute.DefaultValue = defaultDictionary; } #endregion if (Type is CustomItemType) { CustomItemType customType = (CustomItemType)Type; customType.GetCustomType().WriteDefaultValueToXElement(SerializedDefaultValue, attribute.DefaultValue); } else { Type.WriteToXElement(SerializedDefaultValue, attribute.DefaultValue); } }
/// <summary> /// Deserializes values of configuration items from xml document and inject them into an object. /// </summary> /// <param name="targetObject">A target object where values will be set.</param> /// <param name="config">Source xml document.</param> public static void LoadConfiguration(object targetObject, XDocument config) { System.Diagnostics.Debug.Assert(config != null); System.Diagnostics.Debug.Assert(config.Root != null); Type targetType = targetObject.GetType(); ClassMetadata classMetadata = new ClassMetadata(targetType); List<ItemMetadata> unsetItems = new List<ItemMetadata>(classMetadata.ConfigurableItems); var xItems = config.Root.Elements(XName_ItemElement); foreach (XElement xItem in xItems) { string name = xItem.Attribute(XName_ItemNameAttribute).Value; ItemMetadata item = classMetadata.ConfigurableItems.FirstOrDefault(i => i.Name == name); if (item != null) { unsetItems.Remove(item); object value = item.ReadFromXElement(xItem); item.SetValue(targetObject, value); } } #region Set default values for unset items foreach (ItemMetadata item in unsetItems) { object defaultValue; defaultValue = item.GetDefaultValue(); item.SetValue(targetObject, defaultValue); } #endregion }
/// <summary> /// Creates a WPF control for editing configuration items of given type. /// </summary> /// <param name="classMetadata">A description of a type which configuration items will be edited.</param> /// <returns>A new item editor.</returns> public static ConfigurationEditor CreateEditor(ClassMetadata classMetadata) { var result = new ConfigurationEditor(classMetadata); return result; }
/// <summary> /// Creates a WPF control for editing configuration items of given type. /// </summary> /// <param name="targetType">A type which configuration items will be edited.</param> /// <returns>A new item editor.</returns> public static ConfigurationEditor CreateEditor(Type targetType) { ClassMetadata classMetadata = new ClassMetadata(targetType); return CreateEditor(classMetadata); }
/// <summary> /// Gets values of configration items from object and serialize them into xml document. /// </summary> /// <param name="sourceObject">Object which values will be saved.</param> /// <returns>An xml document containing serialized values of configuration items.</returns> public static XDocument SaveConfiguration(object sourceObject) { Type targetType = sourceObject.GetType(); ClassMetadata classMetadata = new ClassMetadata(targetType); XElement xConfig = new XElement(XName_RootElement); foreach (ItemMetadata item in classMetadata.ConfigurableItems) { XElement xItem = new XElement(XName_ItemElement); xItem.SetAttributeValue(XName_ItemNameAttribute, item.Name); object value = item.GetValue(sourceObject); item.WriteToXElement(xItem, value); xConfig.Add(xItem); } XDocument result = new XDocument(); result.Add(xConfig); return result; }
/// <summary> /// Constructs a metadata description for given property. /// </summary> /// <param name="owner">Metadata of owning class.</param> /// <param name="property">Property representing configurable item.</param> /// <param name="attribute">Attribute of configurable item attached to the property.</param> /// <param name="validators">Required validators for configuration item.</param> public ItemMetadata(ClassMetadata owner, PropertyInfo property, ConfigurationItemAttribute attribute, IEnumerable<ValueValidatorAttribute> validators) : this(owner, ItemMemberKind.Property, property.PropertyType, property.Name, attribute, validators) { }
/// <summary> /// Constructs a metadata description for given field. /// </summary> /// <param name="owner">Metadata of owning class.</param> /// <param name="field">Field representing configurable item.</param> /// <param name="attribute">Attribute of configurable item attached to the field.</param> /// <param name="validators">Required validators for configuration item.</param> public ItemMetadata(ClassMetadata owner, FieldInfo field, ConfigurationItemAttribute attribute, IEnumerable<ValueValidatorAttribute> validators) : this(owner, ItemMemberKind.Field, field.FieldType, field.Name, attribute, validators) { }