IsArray() public static method

public static IsArray ( IContractResolver resolver, Type type ) : bool
resolver IContractResolver
type System.Type
return bool
Ejemplo n.º 1
0
            public KnockoutObservableArray(IContractResolver resolver, object array)
            {
                if (array == null)
                {
                    throw new ArgumentNullException("array");
                }
                if (!JsonValue.IsArray(resolver, array.GetType()))
                {
                    throw new ArgumentException("array must be a recognized Json.NET array type.", "array");
                }

                _array = array;
            }
Ejemplo n.º 2
0
        static JsonValue ToViewModelInternal(Stack <object> visited, Type modelType, IContractResolver resolver, object model)
        {
            if (JsonValue.IsPrimitive(resolver, modelType))
            {
                return(new JsonValue(resolver, model));
            }
            else
            {
                var dicType  = typeof(ViewDataDictionary <>).MakeGenericType(modelType);
                var viewData = (ViewDataDictionary)Activator.CreateInstance(dicType, model);
                var metadata = ModelMetadata.FromStringExpression(string.Empty, viewData);
                var json     = JsonObject.Create();

                foreach (var p in metadata.Properties)
                {
                    var vdi = viewData.GetViewDataInfo(p.PropertyName);

                    //not using eval so it doesn't convert to string...
                    var value = vdi.PropertyDescriptor.GetValue(viewData.Model);

                    if (JsonValue.IsPrimitive(resolver, p.ModelType))
                    {
                        json.AddObservable(p.PropertyName, value);
                    }
                    else if (JsonValue.IsArray(resolver, p.ModelType))
                    {
                        //its an enumerable (we test the property type instead of the value incase the value is null)
                        json.AddObservableArray(p.PropertyName, (IEnumerable)value, resolver);
                    }
                    else
                    {
                        if (!visited.Contains(value))
                        {
                            visited.Push(value);
                            json.AddProperty(p.PropertyName, ToViewModelInternal(visited, p.ModelType, resolver, value));
                            visited.Pop();
                        }
                    }
                }

                return(json);
            }
        }