public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { result = null; bool found = false; //Nice if (indexes.Length == 2 && indexes[0] is string && indexes[1] is int) { var allNodes = _element.Elements(indexes[0].ToString()); int index = (int)indexes[1]; if (index < allNodes.Count()) { result = new DynamicXElement(allNodes.ElementAt(index)); } else { result = new DynamicXElement(null); found = false; return(found); } found = true; } return(found); }
public override bool TryGetMember(GetMemberBinder binder, out object result) { var res = _element.Element(binder.Name); result = new DynamicXElement(res); if (result != null) { return(true); } return(false); }
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { result = null; if (indexes.Length == 2 && indexes[0].GetType() == typeof(string) && indexes[1].GetType() == typeof(int)) { var res = _element.Elements((string)indexes[0]).ElementAt((int)indexes[1]); result = new DynamicXElement(res); return(true); } return(false); }
public override bool TryGetMember(GetMemberBinder binder, out object result) { bool resultNotNull = false; //What happens if '_element.Element()' returns null? result = new DynamicXElement(_element.Element(binder.Name)); if (result != null) { resultNotNull = true; } return(resultNotNull); }
static void Main(string[] args) { dynamic planets = DynamicXElement.Create(XElement.Load("..\\..\\planets.xml")); var mercury = planets.Planet; // first planet Console.WriteLine(mercury); var venus = planets["Planet", 1]; // second planet Console.WriteLine(venus); var ourMoon = planets["Planet", 2].Moons.Moon; Console.WriteLine(ourMoon); var marsMoon = planets["Planet", 3]["Moons", 0].Moon; // mars second moon Console.WriteLine(marsMoon); }
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { bool isValid = false; if (indexes.Length != 2 || typeof(string) != indexes[0].GetType() || typeof(int) != indexes[1].GetType()) { result = null; } else { //What happens if '_element.Elements((string)indexes[0])' returns an empty collection? result = new DynamicXElement(_element.Elements((string)indexes[0]).ElementAt((int)indexes[1])); isValid = true; } return(isValid); }
public override bool TryGetMember(GetMemberBinder binder, out object result) { XElement getElement = _element.Element(binder.Name); if (getElement != null) { result = new DynamicXElement(getElement); return(true); } else { result = null; return(false); } }
static void Main(string[] args) { //Absolute file paths are rarely a good idea dynamic planets = DynamicXElement.Create(XElement.Load(@"C:\Users\danny_000\Documents\Visual Studio 2015\Projects\DynamicXml\DynamicXml\planets.xml")); var mercury = planets.Planet; // first planet Console.WriteLine(mercury); var venus = planets["Planet", 1]; // second planet Console.WriteLine(venus); var ourMoon = planets["Planet", 2].Moons.Moon; Console.WriteLine(ourMoon); var marsMoon = planets["Planet", 3]["Moons", 0].Moon; // mars second moon Console.WriteLine(marsMoon); }
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { if (indexes.Length != 2 || typeof(string) != indexes[0].GetType() || typeof(int) != indexes[1].GetType()) { result = null; return(false); } var collectionElements = _element.Elements((string)indexes[0]); //What happens if 'collectionElements' is empty? var res = collectionElements.ElementAt((int)indexes[1]); result = new DynamicXElement(res); if (result == null) { return(false); } return(true); }
static void Main(string[] args) { dynamic planets = DynamicXElement.Create(XElement.Load("Plantes.xml")); var mercury = planets.Planet; Console.WriteLine(mercury); var venus = planets["Planet", 1]; Console.WriteLine(venus); var ourMoon = planets["Planet", 2].Moons.Moon; Console.WriteLine(ourMoon); var marsMoon = planets["Planet", 3]["Moons", 0].Moon; Console.WriteLine(marsMoon); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(planets.ToString()); Console.WriteLine(); Console.WriteLine(); planets["Planet", 0]["Name", 0] = "Mercury1"; planets["Planet", 1]["Name", 0] = "NewVenus"; planets["Planet", 3]["Moons", 0]["Moon", 0] = "NewPhobos"; Console.WriteLine(planets.Planet); Console.WriteLine(planets["Planet", 1]); Console.WriteLine(planets["Planet", 3]["Moons", 0].Moon); Console.WriteLine(); Console.WriteLine(); planets.Planet.Name = "Abra"; Console.WriteLine(planets["Planet", 0]["Name", 0]); planets.Planet = "NewPlanet"; Console.WriteLine(planets.ToString()); }
public override bool TryGetMember(GetMemberBinder binder, out object result) { result = new DynamicXElement(_element.Element(binder.Name)); return(result != null); }
public static dynamic Create(XElement element) { DynamicXElement dynamicXElement = new DynamicXElement(element); return(dynamicXElement); }