/// <summary> /// Converts any object into an <see cref="Expression" /> object. /// </summary> /// <param name="value"></param> /// <returns></returns> public static Expression FromObject(object value) { Expression result = value as Expression; if (result != null) { return(result); } IEnumerable enumerable; IDictionary dictionary; string str; double d; if (value == null) { result = JS.Null(); } else if ((str = value as string) != null) { result = FromString(str); } else if (value is Statement) { throw new InvalidOperationException("A statement cannot be used as an expression."); } else if ((dictionary = value as IDictionary) != null) { result = ObjectLiteralExpression.FromDictionary(dictionary); } else if ((enumerable = value as IEnumerable) != null) { result = JS.ArrayOrObject(enumerable); } else if (value.GetType().IsClass) { result = JS.Object(JS.GetValues(value)); } else if (value is Boolean) { result = FromBoolean((bool)value); } else if (double.TryParse(Convert.ToString(value, CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture, out d)) { result = FromDouble(d); } else { result = FromString(value.ToString()); } return(result); }
/// <summary> /// Creates a new instance of <see cref="ObjectLiteralExpression" />, by copying the specified expression's properties, and adding the specified name and value to the properties. /// </summary> /// <param name="expression">The expression to copy the properties from.</param> /// <param name="name">The name of the property to add.</param> /// <param name="value">The value of the property to add.</param> /// <returns>a new instance of <see cref="ObjectLiteralExpression" /></returns> public static ObjectLiteralExpression WithProperty(this ObjectLiteralExpression expression, Expression name, Expression value) { if (expression == null) { throw new ArgumentNullException("expression"); } ObjectLiteralExpression result = new ObjectLiteralExpression(expression.Properties); result.Properties[name] = value; return(result); }
/// <summary> /// Uses reflection to find out if the specified sequence implements any <see cref="IEnumerable{KeyValuePair}" /> /// and returns an <see cref="ObjectLiteralExpression" /> in that case, or an <see cref="ArrayExpression" /> if not. /// </summary> /// <param name="enumerable">The sequence to convert.</param> /// <returns>Either an instance of <see cref="ObjectLiteralExpression" /> or <see cref="ArrayExpression" />.</returns> public static Expression ArrayOrObject(IEnumerable enumerable) { Type keyValuePairEnumerableType = GetObjectsFirstIEnumerableKeyValuePairInterface(enumerable); if (keyValuePairEnumerableType == null) { return(Array(enumerable)); } var keyProperty = keyValuePairEnumerableType.GetProperty("Key"); var valueProperty = keyValuePairEnumerableType.GetProperty("Value"); return(ObjectLiteralExpression.FromDictionary <object, object>(enumerable.Cast <object>().ToDictionary( pair => keyProperty.GetValue(pair, null), pair => valueProperty.GetValue(pair, null)))); }
/// <summary> /// Creates a new instance of <see cref="ObjectLiteralExpression" />, by copying the specified expression's properties, and adding the specified properties. /// </summary> /// <param name="expression">The expression to copy the properties from.</param> /// <param name="values">A dictionary containing properties to add to the new instance.</param> /// <returns>a new instance of <see cref="ObjectLiteralExpression" /></returns> public static ObjectLiteralExpression WithProperties(this ObjectLiteralExpression expression, IDictionary <Expression, Expression> values) { if (expression == null) { throw new ArgumentNullException("expression"); } if (values == null) { throw new ArgumentNullException("values"); } ObjectLiteralExpression result = new ObjectLiteralExpression(expression.Properties); foreach (KeyValuePair <Expression, Expression> property in values) { result.Properties[property.Key] = property.Value; } return(result); }
/// <summary> /// Creates a new instance of <see cref="ObjectLiteralExpression" /> representing the keys and values of the specified <see cref="IDictionary{K,V}" />. /// </summary> /// <typeparam name="TKey">The type of Key.</typeparam> /// <typeparam name="TValue">The type of Value.</typeparam> /// <param name="dictionary">The dictionary from which to extract the keys and values to represent.</param> /// <returns>A new instance of <see cref="ObjectLiteralExpression" />.</returns> public static ObjectLiteralExpression FromDictionary <TKey, TValue>(IDictionary <TKey, TValue> dictionary) { if (dictionary == null) { throw new ArgumentNullException("dictionary"); } ObjectLiteralExpression result = new ObjectLiteralExpression(); foreach (var pair in dictionary) { Expression key = pair.Key as Expression ?? (JS.IsValidIdentifier(pair.Key.ToString()) ? JS.Id(pair.Key.ToString()) : FromString(pair.Key.ToString())); result.Properties.Add(key, FromObject(pair.Value)); } return(result); }
/// <summary> /// Initializes a new instance of <see cref="ObjectLiteralExpression" /> that defines the specified properties. /// </summary> /// <param name="properties"></param> public static ObjectLiteralExpression FromDictionary(IDictionary properties) { if (properties == null) { throw new ArgumentNullException("properties"); } var result = new ObjectLiteralExpression(); foreach (DictionaryEntry property in properties) { Expression key = property.Key as Expression ?? (JS.IsValidIdentifier(property.Key.ToString()) ? JS.Id(property.Key.ToString()) : FromString(property.Key.ToString())); result.Properties.Add(key, FromObject(property.Value)); } return(result); }