private object EntryValue(string entry)
        {
            var entryType  = _dictionary.GetType().GenericTypeArguments[0];
            var entryValue = EntryConvert.ToObject(entryType, entry, _serialization.FormatProvider);

            return(entryValue);
        }
Esempio n. 2
0
        /// <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));
            }
        }
 /// <summary>
 /// Read value from config
 /// </summary>
 protected virtual object ReadFromConfig(Entry entry)
 {
     // Synchronous resolution
     return(EntryConvert.ToObject(Property.PropertyType, entry.Value.Current, FormatProvider));
 }