/// <see cref="ICustomSerialization"/> public virtual EntryPrototype[] Prototypes(Type memberType, ICustomAttributeProvider attributeProvider) { // Check if it is a list, array or dictionary if (EntryConvert.IsCollection(memberType)) { memberType = EntryConvert.ElementType(memberType); } object prototype; if (memberType == typeof(string)) { prototype = string.Empty; } else { prototype = Activator.CreateInstance(memberType); ValueProviderExecutor.Execute(prototype, new ValueProviderExecutorSettings().AddDefaultValueProvider()); } return(new[] { new EntryPrototype(memberType.Name, prototype) }); }
/// <see cref="ICustomSerialization"/> public virtual EntryPrototype[] Prototypes(Type memberType, ICustomAttributeProvider attributeProvider) { // Check if it is a list, array or dictionary if (EntryConvert.IsCollection(memberType)) { memberType = EntryConvert.ElementType(memberType); } List <EntryPrototype> prototypes = new List <EntryPrototype>(); if (memberType == typeof(string)) { prototypes.Add(new EntryPrototype(nameof(String), string.Empty)); } else if (memberType.IsEnum) { foreach (Enum enumValue in Enum.GetValues(memberType)) { prototypes.Add(new EntryPrototype(enumValue.ToString("G"), enumValue)); } } else { var prototype = Activator.CreateInstance(memberType); if (memberType.IsClass) { ValueProviderExecutor.Execute(prototype, new ValueProviderExecutorSettings().AddDefaultValueProvider()); } prototypes.Add(new EntryPrototype(memberType.Name, prototype)); } return(prototypes.ToArray()); }
private object EntryValue(string entry) { var entryType = _dictionary.GetType().GenericTypeArguments[0]; var entryValue = EntryConvert.ToObject(entryType, entry, _serialization.FormatProvider); return(entryValue); }
/// <see cref="ICustomSerialization"/> public virtual object CreateInstance(Type memberType, ICustomAttributeProvider attributeProvider, Entry encoded) { if (EntryConvert.IsCollection(memberType)) { memberType = EntryConvert.ElementType(memberType); } return(CreateInstance(memberType, encoded)); }
/// <summary> /// Create entry for object from collection /// </summary> public static Entry CreateSub(object item, int index, ICustomSerialization customSerialization) { var subEntry = EntryConvert.EncodeObject(item, customSerialization); subEntry.DisplayName = GetEntryName(item); subEntry.Identifier = index.ToString("D"); return(subEntry); }
/// <see cref="ICustomSerialization"/> public virtual string[] PossibleValues(Type memberType, ICustomAttributeProvider attributeProvider) { // Element type for collections if (EntryConvert.IsCollection(memberType)) { return new[] { EntryConvert.ElementType(memberType).Name } } ; // Names of Enums or null return(memberType.IsEnum ? Enum.GetNames(memberType) : null); }
/// <see cref="ICustomSerialization"/> public virtual object ConvertValue(Type memberType, ICustomAttributeProvider attributeProvider, Entry mappedEntry, object currentValue) { // Other operations depend on the element type switch (mappedEntry.Value.Type) { case EntryValueType.Stream: Stream targetStream; var safeContent = mappedEntry.Value.Current ?? ""; var contentBytes = Convert.FromBase64String(safeContent); var currentStream = currentValue as Stream; var createNewMemoryStream = currentStream == null || !currentStream.CanWrite; if (!createNewMemoryStream && currentStream.GetType() == typeof(MemoryStream) && currentStream.Length < contentBytes.Length) { // MemoryStream is not expandable createNewMemoryStream = true; } if (currentStream != null && !createNewMemoryStream) { if (currentStream.CanSeek) { currentStream.Seek(0, SeekOrigin.Begin); } targetStream = currentStream; targetStream.Write(contentBytes, 0, contentBytes.Length); targetStream.SetLength(contentBytes.Length); } else { currentStream?.Dispose(); targetStream = new MemoryStream(contentBytes); } return(targetStream); case EntryValueType.Class: return(currentValue ?? Activator.CreateInstance(memberType)); case EntryValueType.Collection: return(CollectionBuilder(memberType, currentValue, mappedEntry)); default: var value = mappedEntry.Value.Current; return(value == null ? null : EntryConvert.ToObject(memberType, value, FormatProvider)); } }
/// <see cref="ICustomSerialization"/> public virtual string[] PossibleValues(Type memberType, ICustomAttributeProvider attributeProvider) { // Element type for collections var isCollection = EntryConvert.IsCollection(memberType); if (isCollection) { memberType = EntryConvert.ElementType(memberType); } // Enum names, member name or null return(memberType.IsEnum ? Enum.GetNames(memberType) : isCollection ? new[] { memberType.Name } : null); }
public IEnumerable <Entry> Serialize() { var entries = new List <Entry>(); foreach (var key in _dictionary.Keys) { var value = _dictionary[key]; var keyString = key.ToString(); var subEntry = EntryConvert.EncodeObject(value, _serialization); subEntry.DisplayName = keyString; subEntry.Identifier = keyString; entries.Add(subEntry); } return(entries); }
/// <summary> /// Get name of collection entries /// </summary> public static string GetEntryName(object item) { var itemType = item.GetType(); // Value type do not have names if (EntryConvert.ValueOrStringType(itemType)) { return(itemType.Name); } // Check for display name declaration var displayName = itemType.GetDisplayName(); if (!string.IsNullOrWhiteSpace(displayName)) { return(displayName); } // Check if item declares its own version of ToString() return(itemType.GetMethod(nameof(ToString)).DeclaringType == typeof(object) ? itemType.Name : item.ToString()); }
/// <summary> /// Build collection object from entry /// </summary> protected static object CollectionBuilder(Type collectionType, object currentValue, Entry collectionEntry) { // Arrays must be recreated whenever their size changes if (collectionType.IsArray) { var currentArray = (Array)currentValue; var size = collectionEntry.SubEntries.Count; return(currentArray != null && currentArray.Length == size ? currentArray : Array.CreateInstance(collectionType.GetElementType(), size)); } // Create instance for collections of type Dictionary if (EntryConvert.IsDictionary(collectionType)) { // Use dictionary when interface where used if (collectionType.IsInterface) { collectionType = typeof(Dictionary <,>).MakeGenericType(collectionType.GenericTypeArguments); } // Reuse current object if available return(currentValue ?? Activator.CreateInstance(collectionType)); } // Create instance for collections of type list if (typeof(IEnumerable).IsAssignableFrom(collectionType)) { // Use lists when interfaces where used if (collectionType.IsInterface) { collectionType = typeof(List <>).MakeGenericType(collectionType.GenericTypeArguments); } // Reuse current object if available return(currentValue ?? Activator.CreateInstance(collectionType)); } // Other collections are not supported return(null); }
/// <summary> /// Read value from config /// </summary> protected virtual object ReadFromConfig(Entry entry) { // Synchronous resolution return(EntryConvert.ToObject(Property.PropertyType, entry.Value.Current, FormatProvider)); }
/// <summary> /// Read value from property and write to config /// </summary> public virtual void ReadValue(object source, Entry target) { var value = Property.GetValue(source); target.Value.Current = EntryConvert.ConvertToString(value, FormatProvider) ?? string.Empty; }
/// <see cref="ICustomSerialization"/> public virtual string[] PossibleElementValues(Type memberType, ICustomAttributeProvider attributeProvider) { var elementType = EntryConvert.ElementType(memberType); return(PossibleValues(elementType, attributeProvider)); }