Ejemplo n.º 1
0
        internal void SetSyncProperty(string name, object value)
        {
            EnsureMainThread();

            SyncPropertyDisabled = true;
            try
            {
                SyncProperty property;
                SyncProperties.TryGetValue(name, out property);

                if (property == null)
                {
                    throw new ArgumentException("Sync property not found.", "name");
                }

                try
                {
                    property.SetDelegate.DynamicInvoke(this, ConvertUtility.ChangeType(value, property.SyncPropertyType));
                }
                catch
                {
                    throw new ArgumentException("Invalid sync property value.", "value");
                }
            }
            finally
            {
                SyncPropertyDisabled = false;
            }
        }
        /// <summary>
        /// Deserializes the json value.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="jsonValue">The json formatted value.</param>
        /// <returns>The strongly typed (best effort) value.</returns>
        private static object DeserializeValue(Type type, object jsonValue)
        {
            object value = null;

            if (jsonValue is JToken)
            {
                try
                {
                    value = ((JToken)jsonValue).ToObject(type, serializer);
                }
                catch (JsonException ex)
                {
                    throw new VssPropertyValidationException("Value", PatchResources.InvalidValue(jsonValue, type), ex);
                }
            }
            else
            {
                // Not a JToken, so it must be a primitive type.  Will
                // attempt to convert to the requested type.
                if (type.IsAssignableOrConvertibleFrom(jsonValue))
                {
                    value = ConvertUtility.ChangeType(jsonValue, type);
                }
                else
                {
                    Guid guidValue;
                    if (Guid.TryParse((string)jsonValue, out guidValue))
                    {
                        value = guidValue;
                    }
                    else
                    {
                        throw new VssPropertyValidationException("Value", PatchResources.InvalidValue(jsonValue, type));
                    }
                }
            }

            return(value);
        }
        /// <summary>
        /// Applies the Test patch operation to the target
        /// </summary>
        /// <param name="target">The object to apply the operation to.</param>
        public override void Apply(TModel target)
        {
            this.Apply(
                target,
                (type, parent, current) =>
            {
                object memberValue = null;
                if (type.IsList())
                {
                    var list = (IList)parent;
                    int index;
                    if (int.TryParse(current, out index) &&
                        list.Count > index)
                    {
                        memberValue = list[index];
                    }
                    else
                    {
                        // We can't insert beyond the length of the list.
                        throw new PatchOperationFailedException(PatchResources.IndexOutOfRange(this.Path));
                    }
                }
                else if (type.IsDictionary())
                {
                    var fieldDictionary = ((IDictionary)parent);

                    if (!fieldDictionary.Contains(current))
                    {
                        throw new InvalidPatchFieldNameException(PatchResources.InvalidFieldName(current));
                    }
                    memberValue = fieldDictionary[current];
                }
                else
                {
                    memberValue = type.GetMemberValue(current, parent);
                }

                var success = false;
                if (memberValue != null)
                {
                    if (memberValue is IList)
                    {
                        // TODO: Implement
                        throw new PatchOperationFailedException(PatchResources.TestNotImplementedForList());
                    }
                    else if (memberValue is IDictionary)
                    {
                        // TODO: Implement
                        throw new PatchOperationFailedException(PatchResources.TestNotImplementedForDictionary());
                    }
                    else if (memberValue.GetType().IsAssignableOrConvertibleFrom(this.Value))
                    {
                        // We convert the objects since we need the values unboxed.
                        var convertedMemberValue = ConvertUtility.ChangeType(memberValue, memberValue.GetType());
                        var convertedValue       = ConvertUtility.ChangeType(this.Value, memberValue.GetType());

                        success = convertedMemberValue.Equals(convertedValue);
                    }
                    else
                    {
                        success = memberValue.Equals(this.Value);
                    }
                }
                else
                {
                    success = object.Equals(memberValue, this.Value);
                }

                if (!success)
                {
                    throw new TestPatchOperationFailedException(PatchResources.TestFailed(this.Path, memberValue, this.Value));
                }
            });
        }