Example #1
0
        public bool IsInstanceOfType(object value)
        {
            if (_isInputType)
            {
                if (value is null)
                {
                    return(true);
                }

                if (ClrType.IsInstanceOfType(value))
                {
                    return(true);
                }

                Type elementType = DotNetTypeInfoFactory
                                   .GetInnerListType(value.GetType());

                if (elementType == null)
                {
                    return(false);
                }

                return(elementType == ElementType.ToClrType());
            }

            // TODO : resources
            throw new InvalidOperationException(
                      "The specified type is not an input type.");
        }
Example #2
0
        protected sealed override bool IsInstanceOfType(object value)
        {
            if (value is null)
            {
                return(true);
            }

            if (ClrType.IsInstanceOfType(value))
            {
                return(true);
            }

            Type elementType = DotNetTypeInfoFactory.GetInnerListType(value.GetType());

            if (elementType is null)
            {
                return(false);
            }

            Type clrType = InnerClrType;

            if (elementType == typeof(object))
            {
                return(value is IList l &&
                       (l.Count == 0 || clrType == l[0]?.GetType()));
            }

            return(elementType == clrType);
        }
        protected override void VisitList(
            IList <object> list,
            DeserializationContext context)
        {
            if (context.Type.IsArray)
            {
                var array = Array.CreateInstance(
                    context.Type.GetElementType(),
                    list.Count);

                for (int i = 0; i < list.Count; i++)
                {
                    var valueContext = new DeserializationContext();
                    valueContext.Type = context.Type.GetElementType();
                    Visit(list[i], valueContext);

                    array.SetValue(valueContext.Object, i);
                }

                context.Object = array;
            }
            else
            {
                Type elementType = DotNetTypeInfoFactory.GetInnerListType(context.Type);
                if (elementType != null)
                {
                    Type  listType = typeof(List <>).MakeGenericType(elementType);
                    IList l        = (IList)Activator.CreateInstance(listType);

                    for (int i = 0; i < list.Count; i++)
                    {
                        var valueContext = new DeserializationContext();
                        valueContext.Type = context.Type.GetElementType();
                        Visit(list[i], valueContext);

                        list.Add(valueContext.Object);
                    }
                }
            }
        }