示例#1
0
        /// <summary>
        /// Tries to retrieve the value from the given path. The path is a dot separated string of property names.
        /// </summary>
        /// <typeparam name="T">The expected type of the value.</typeparam>
        /// <param name="instance">The root instance to start the lookup from.</param>
        /// <param name="path">A dot separated string of property names. Spreaded properties can be indexed using [N] for example "MySpread[0]" retrieves the first value in MySpread.</param>
        /// <param name="defaultValue">The default value to use in case the lookup failed.</param>
        /// <param name="value">The returned value.</param>
        /// <returns>True if the lookup succeeded.</returns>
        public static bool TryGetValueByPath <T>(this IVLObject instance, string path, T defaultValue, out T value)
        {
            var spine = instance.GetSpine(path);
            var entry = spine.Pop();
            var leaf  = entry.Value;

            return(TryGetValueByExpression(leaf, entry.Key, defaultValue, out value));
        }
示例#2
0
        static IVLObject WithValueByPath <T>(this IVLObject instance, string path, T value)
        {
            var spine = instance.GetSpine(path);

            // Update the leaf with the value
            var entry = spine.Pop();
            var leaf  = entry.Value;

            var updatedInstance = leaf.WithValueFromExpression(entry.Key, value);

            // Update the VL objects along the spine
            while (spine.Count > 0)
            {
                entry           = spine.Pop();
                updatedInstance = entry.Value.WithValueFromExpression(entry.Key, updatedInstance);
            }

            return(updatedInstance);
        }