Exemple #1
0
        /// <summary>
        ///   Gets the object value contained at this node, if any.
        ///   Its type can be known by checking the <see cref="Type"/>
        ///   property of this node.
        /// </summary>
        ///
        /// <typeparam name="T">The object type, if known.</typeparam>
        ///
        /// <returns>The object stored at this node.</returns>
        ///
        public T GetValue <T>()
        {
            if (Value is T)
            {
                return((T)Value);
            }

            if (typeof(T).IsArray)
            {
                var   targetType = typeof(T).DeclaringType;
                Array src        = Value as Array;
                Array dst        = Array.CreateInstance(targetType, dimensions);

                foreach (int[] idx in Indices.From(src))
                {
                    dst.SetValue(Convert.ChangeType(src.GetValue(idx), targetType), idx);
                }

                return((T)Convert.ChangeType(dst, typeof(T)));
            }

            throw new InvalidCastException();
        }