Beispiel #1
0
        //---------------------------------------------------------------------------------------//
        // Public Interface
        //---------------------------------------------------------------------------------------//

        #region Public Interface

        /// <summary>
        /// Indexer that returns XAttribute wrapper by XNode
        /// </summary>
        /// <example>
        /// You have several choises how to deal with attributes.
        ///
        /// // 1
        /// XElement element = new XElement("name", new XAttribute("attr", "val"));
        /// dynamic dynamicElement = element.AsDynamic();
        /// string value = dynamicElement["attr"];
        ///
        /// // 2
        /// XElement element = new XElement("name", new XAttribute("attr", "val"));
        /// dynamic dynamicElement = element.AsDynamic();
        /// XAttribute attribute = dynamicElement["attr"];
        /// string value = attribute.Value;
        /// string theSameValue = (string)attribute;
        ///
        /// // 3
        /// XElement element = new XElement("name", new XAttribute("attr", "val"));
        /// dynamic dynamicElement = element.AsDynamic();
        /// string value = (string)dynamicElement["attr"];
        /// </example>
        public dynamic this[XName name]
        {
            get
            {
                Contract.Requires(name != null);

                XAttribute attribute = element.Attribute(name);

                if (attribute == null)
                {
                    throw new InvalidOperationException("Attribute not found. Name: " + name.LocalName);
                }

                return(attribute.AsDynamic());
            }
        }