Exemplo n.º 1
0
        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
        {
            int ndx = (int)indexes[0];

            result = new DynamicXml(_elements[ndx]);
            return(true);
        }
Exemplo n.º 2
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            /* handle the Value and Count special cases */
            if (binder.Name == "Value")
            {
                result = _elements[0].Value;
            }
            else if (binder.Name == "Count")
            {
                result = _elements.Count;
            }
            else
            {
                /* try to find a named attribute first */
                var attr = _elements[0].Attribute(XName.Get(binder.Name));
                if (attr != null)
                {
                    /* if a named attribute was found, return that NON-dynamic object */
                    result = attr;
                }
                else
                {
                    /* find the named descendants */
                    var items = _elements.Descendants(XName.Get(binder.Name));
                    if (items != null && items.Count() > 0)
                    {
                        /* prepare a new dynamic object with the list of found descendants */
                        result = new DynamicXml(items);
                    }
                }
            }
            if (result == null)
            {
                /* not found, create a new element here */
                _elements[0].AddFirst(new XElement(binder.Name));
                result = new DynamicXml(_elements[0].Descendants().First());
            }
            return(true);
        }