示例#1
0
        /// <summary>
        /// This methods returns all assets that would be changed when trying to change this asset.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="action">The action.</param>
        /// <param name="value">The value.</param>
        /// <returns>LoggerResult.</returns>
        /// <exception cref="System.ArgumentNullException">path</exception>
        public List <AssetItem> FindAssetsFromChange(MemberPath path, MemberPathAction action, object value)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            var result = new List <AssetItem>();

            FindAssetsFromChange(path, action, value, result);
            return(result);
        }
示例#2
0
        private void FindAssetsFromChange(MemberPath path, MemberPathAction action, object value, List <AssetItem> items)
        {
            object oldValue;
            var    pathSucceeded = path.TryGetValue(Asset, out oldValue);

            // If the path exists and value changed or we are doing another operation (remove key...etc.)
            // then add the items as a list of item to change
            if (pathSucceeded && (action != MemberPathAction.ValueSet || value != oldValue))
            {
                items.Add(this);
            }

            if (Package != null && Package.Session != null)
            {
                var itemsToDetect = Package.Session.DependencyManager.FindAssetsInheritingFrom(Id);
                foreach (var item in itemsToDetect)
                {
                    item.FindAssetsFromChange(path, action, value, items);
                }
            }
        }
示例#3
0
 /// <summary>
 /// Set the asset member.
 /// </summary>
 /// <param name="assetMember">The asset member to set</param>
 /// <param name="action">The action to perform on the member</param>
 /// <param name="value">The value to set to the member</param>
 /// <returns>The list of inheriting members that should to be reset</returns>
 public IEnumerable<AssetMember> Set(AssetMember assetMember, MemberPathAction action, object value)
 {
     return Set(assetMember.Asset, assetMember.MemberPath, action, value);
 }
示例#4
0
 /// <summary>
 /// Set the value of an the asset member.
 /// </summary>
 /// <param name="asset">The asset to set</param>
 /// <param name="path">The path to the member to set</param>
 /// <param name="action">The action to perform on the member</param>
 /// <param name="value">The value to set to the member</param>
 /// <returns>The list of inheriting members that should to be reset</returns>
 public IEnumerable<AssetMember> Set(Asset asset, MemberPath path, MemberPathAction action, object value)
 {
     return Enumerable.Empty<AssetMember>();
 }
示例#5
0
        public bool Apply([NotNull] object rootObject, MemberPathAction actionType, object value)
        {
            if (rootObject == null)
            {
                throw new ArgumentNullException(nameof(rootObject));
            }
            if (rootObject.GetType().IsValueType)
            {
                throw new ArgumentException("Value type for root objects are not supported", nameof(rootObject));
            }
            if (actionType != MemberPathAction.ValueSet && actionType != MemberPathAction.CollectionAdd && value != null)
            {
                throw new ArgumentException("Value must be null for action != (MemberActionType.SetValue || MemberPathAction.CollectionAdd)");
            }

            if (items == null || items.Count == 0)
            {
                throw new InvalidOperationException("This instance doesn't contain any path. Use Push() methods to populate paths");
            }

            var lastItem = items[items.Count - 1];

            switch (actionType)
            {
            case MemberPathAction.CollectionAdd:
                if (!(lastItem is CollectionPathItem))
                {
                    throw new ArgumentException("Invalid path [{0}] for action [{1}]. Expecting last path to be a collection item".ToFormat(this, actionType));
                }
                break;

            case MemberPathAction.CollectionRemove:
                if (!(lastItem is CollectionPathItem) && !(lastItem is ArrayPathItem))
                {
                    throw new ArgumentException("Invalid path [{0}] for action [{1}]. Expecting last path to be a collection/array item".ToFormat(this, actionType));
                }
                break;

            case MemberPathAction.DictionaryRemove:
                if (!(lastItem is DictionaryPathItem))
                {
                    throw new ArgumentException("Invalid path [{0}] for action [{1}]. Expecting last path to be a dictionary item".ToFormat(this, actionType));
                }
                break;
            }

            var stack = stackTLS;

            try
            {
                object nextObject = rootObject;

                if (stack == null)
                {
                    stack    = new List <object>();
                    stackTLS = stack;
                }
                else
                {
                    stack.Clear();
                }

                stack.Add(nextObject);
                for (int i = 0; i < items.Count - 1; i++)
                {
                    var item = items[i];
                    nextObject = item.GetValue(nextObject);
                    stack.Add(nextObject);
                }

                if (actionType == MemberPathAction.ValueClear)
                {
                    if (lastItem is CollectionPathItem)
                    {
                        actionType = MemberPathAction.CollectionRemove;
                    }
                    else if (lastItem is DictionaryPathItem)
                    {
                        actionType = MemberPathAction.DictionaryRemove;
                    }
                    else
                    {
                        actionType = MemberPathAction.ValueSet;
                    }
                }

                switch (actionType)
                {
                case MemberPathAction.ValueSet:
                    lastItem.SetValue(stack, stack.Count - 1, nextObject, value);
                    break;

                case MemberPathAction.DictionaryRemove:
                    ((DictionaryPathItem)lastItem).Descriptor.Remove(nextObject, ((DictionaryPathItem)lastItem).Key);
                    break;

                case MemberPathAction.CollectionAdd:
                    ((CollectionPathItem)lastItem).Descriptor.Add(nextObject, value);
                    break;

                case MemberPathAction.CollectionRemove:
                    ((CollectionPathItem)lastItem).Descriptor.RemoveAt(nextObject, ((CollectionPathItem)lastItem).Index);
                    break;
                }
            }
            catch (Exception)
            {
                // If an exception occurred, we cannot resolve this member path to a valid property/field
                return(false);
            }
            finally
            {
                stack?.Clear();
            }
            return(true);
        }
示例#6
0
 /// <summary>
 /// Set the asset member.
 /// </summary>
 /// <param name="assetMember">The asset member to set</param>
 /// <param name="action">The action to perform on the member</param>
 /// <param name="value">The value to set to the member</param>
 /// <returns>The list of inheriting members that should to be reset</returns>
 public IEnumerable <AssetMember> Set(AssetMember assetMember, MemberPathAction action, object value)
 {
     return(Set(assetMember.Asset, assetMember.MemberPath, action, value));
 }
示例#7
0
 /// <summary>
 /// Set the value of an the asset member.
 /// </summary>
 /// <param name="asset">The asset to set</param>
 /// <param name="path">The path to the member to set</param>
 /// <param name="action">The action to perform on the member</param>
 /// <param name="value">The value to set to the member</param>
 /// <returns>The list of inheriting members that should to be reset</returns>
 public IEnumerable <AssetMember> Set(Asset asset, MemberPath path, MemberPathAction action, object value)
 {
     return(Enumerable.Empty <AssetMember>());
 }