Example #1
0
            public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
            {
                if (target != null && errorSuggestion == null)
                {
                    string     exceptionMessage = SG.GetString(SR.DynamicPropertyNotDefined, target.LimitType, this.Name);
                    Expression throwExpression  = Expression.Throw(Expression.Constant(new InvalidOperationException(exceptionMessage)), typeof(object));

                    errorSuggestion = new DynamicMetaObject(throwExpression, target.Restrictions);
                }

                return(errorSuggestion);
            }
Example #2
0
        /// <summary>
        /// Extension method for converting a <see cref="JsonValue"/> collection into an array of objects.
        /// </summary>
        /// <param name="jsonValue">The <see cref="JsonValue"/> instance to be converted to an object array.</param>
        /// <returns>An array of objects represented by the specified <see cref="JsonValue"/> instance.</returns>
        /// <remarks>The <see cref="JsonType"/> value of the specified <see cref="JsonValue"/> instance must be <see cref="JsonType.Array"/>.</remarks>
        public static object[] ToObjectArray(this JsonValue jsonValue)
        {
            if (jsonValue == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("jsonValue");
            }

            if (jsonValue.JsonType != JsonType.Array)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SG.GetString(SR.OperationNotSupportedOnJsonType, jsonValue.JsonType)));
            }

            return(ToClrCollection <object[]>(jsonValue));
        }
Example #3
0
        /// <summary>
        /// Extension method for converting a <see cref="JsonValue"/> collection into a dictionary of <see cref="string"/>/<see cref="object"/> key/value pairs.
        /// </summary>
        /// <param name="jsonValue">The <see cref="JsonValue"/> instance to be converted to a dictionary of <see cref="string"/>/<see cref="object"/> key/value pairs.</param>
        /// <returns>An <see cref="IDictionary{T1,T2}"/> of <see cref="string"/>/<see cref="object"/> key/value pairs represented by the specified <see cref="JsonValue"/> instance.</returns>
        /// <remarks>The <see cref="JsonType"/> value of the specified <see cref="JsonValue"/> instance must be <see cref="JsonType.Object"/>.</remarks>
        public static IDictionary <string, object> ToDictionary(this JsonValue jsonValue)
        {
            if (jsonValue == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("jsonValue");
            }

            if (jsonValue.JsonType != JsonType.Object)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SG.GetString(SR.OperationNotSupportedOnJsonType, jsonValue.JsonType)));
            }

            return(ToClrCollection <Dictionary <string, object> >(jsonValue));
        }
Example #4
0
        /// <summary>
        /// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into an instance of the specified type.
        /// </summary>
        /// <param name="jsonValue">The <see cref="JsonValue"/> instance this method extension is to be applied to.</param>
        /// <param name="type">The type to which the conversion is being performed.</param>
        /// <returns>An object instance initialized with the <see cref="System.Json.JsonValue"/> value
        /// specified if the conversion.</returns>
        /// <exception cref="System.NotSupportedException">If this <see cref="System.Json.JsonValue"/> value cannot be
        /// converted into the type T.</exception>
        public static object ReadAsType(this JsonValue jsonValue, Type type)
        {
            if (jsonValue == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("jsonValue"));
            }

            if (type == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
            }

            object result;

            if (JsonValueExtensions.TryReadAsType(jsonValue, type, out result))
            {
                return(result);
            }

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SG.GetString(SR.CannotReadAsType, jsonValue.GetType().FullName, type.FullName)));
        }