示例#1
0
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            reader.Read();
            var    typeName = reader.ReadAsString();
            object instance;

            if (typeName.StartsWith(nameof(IEnumerable)))
            {
                var splitType    = typeName.Split(Constants.EnumerableOpenTag, StringSplitOptions.RemoveEmptyEntries)[1].Replace(Constants.EnumerableCloseTag, string.Empty).Trim();
                var type         = AssemblyManager.FindType(splitType);
                var resolvedType = DIResolver.GetImplementationType(type);
                instance = Activator.CreateInstance(typeof(List <>).MakeGenericType(resolvedType));
            }
            else
            {
                var type = AssemblyManager.FindType(typeName);
                instance = DIResolver.Get(type);
            }

            reader.Read();
            var propName = reader.ReadAsString();

            reader.Read();
            reader.Read();
            serializer.Populate(reader, instance);

            reader.Read();

            return(new StoreItem()
            {
                Name = propName, Value = instance, Type = typeName
            });
        }
示例#2
0
 /// <summary>
 /// Determines whether this instance can convert the specified object type.
 /// </summary>
 /// <param name="objectType">Type of the object.</param>
 /// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns>
 public override bool CanConvert(Type objectType)
 {
     if (!typeof(IEnumerable).IsAssignableFrom(objectType))
     {
         var registration = DIResolver.GetImplementationType(objectType);
         return(registration != null);
     }
     return(false);
 }