public override bool TryGetMember(GetMemberBinder binder, out object result) { //Go ahead and try to fetch all of the elements matching the member name, and wrap them var elements = BaseElement.Elements(binder.Name); if (HandleIEnumerableXElement(elements, out result)) { return(true); } else { //Ok, so no elements matched, so lets try attributes var attributes = BaseElement.Attributes(binder.Name).Select(attr => attr.Value); int count = attributes.Count(); if (count > 0) { if (count > 1) { result = attributes; //more than one attribute matched, lets return the collection } else { result = attributes.FirstOrDefault(); //only one attribute matched, lets just return it } return(true); // return true because we matched } } return(base.TryGetMember(binder, out result)); }
public override bool TryGetMember(GetMemberBinder binder, out object result) { if (BaseElement == null || binder == null) { result = null; return(false); } //Go ahead and try to fetch all of the elements matching the member name, and wrap them var elements = BaseElement.Elements(binder.Name); if (!elements.Any() && BaseElement.Name == "root" && BaseElement.Elements().Count() == 1) { //no elements matched, lets try first child elements = BaseElement.Elements().ElementAt(0).Elements(binder.Name); } if (HandleIEnumerableXElement(elements, out result)) { return(true); } else { //Ok, so no elements matched, so lets try attributes IEnumerable <string> attributes = BaseElement.Attributes(binder.Name).Select(attr => attr.Value); int count = attributes.Count(); if (count > 0) { if (count > 1) { result = attributes; //more than one attribute matched, lets return the collection } else { result = attributes.FirstOrDefault(); //only one attribute matched, lets just return it } return(true); // return true because we matched } else { //no attributes matched, lets try first child if (BaseElement.Name == "root" && BaseElement.Elements().Count() == 1) { attributes = BaseElement.Elements().ElementAt(0).Attributes(binder.Name).Select(attr => attr.Value); count = attributes.Count(); if (count > 1) { result = attributes; //more than one attribute matched, lets return the collection } else { result = attributes.FirstOrDefault(); //only one attribute matched, lets just return it } return(true); // return true because we matched } } } return(base.TryGetMember(binder, out result)); }