public object Parse(Type type, XObject config, bool isAssignableTypeAllowed, XmlLocation childLocation, IContext context) { var configElement = config as XElement; if (configElement == null) { throw new TestLibsException($"Couldn't parse type: {type} from following config (element node expected):\n{config}"); } var xmlType = _xmlType.Value; string uniqueName = null; object createdObject = null; if (context != null) { createdObject = new XmlBaseType(); var xmlNode = _uniqProperty.Value.Location.Value.GetElement(configElement); if (xmlNode != null) { uniqueName = XmlParser.Parse(_uniqProperty.Value.PropertyType, xmlNode) as string; } if (uniqueName != null) { if (context.Contains(type, uniqueName)) { return(context.ResolveValue(type, uniqueName)); } } } if (isAssignableTypeAllowed) { var possibleTypeName = configElement.Name.ToString(); var assignableXmlTypes = ReflectionManager.GetAssignableTypes(type); foreach (var assignableXmlType in assignableXmlTypes) { if (assignableXmlType.Location.Check(XmlLocationType.Element, possibleTypeName)) { xmlType = assignableXmlType; } } if (xmlType == _xmlType.Value) { throw new TestLibsException($"Couldn't find any type that could be parsed from element with name: {possibleTypeName}\nPossible element names: {assignableXmlTypes.Select(xt => $"Type: {xt.XType}, description: {xt.Description}\n{xt.Location}").ToList().ToStringWithList("=")}"); } } else { xmlType = ReflectionManager.GetXmlType(type); } var parsedPropertiesDict = new Dictionary <XmlProperty, bool>(); xmlType.XmlProperties.ForEach(p => parsedPropertiesDict.Add(p, false)); createdObject = Activator.CreateInstance(xmlType.XType); if (uniqueName != null) { _uniqProperty.Value.SetValue(createdObject, uniqueName); parsedPropertiesDict[_uniqProperty.Value] = true; } var propertyToParse = parsedPropertiesDict.Keys.FirstOrDefault(k => !parsedPropertiesDict[k]); while (propertyToParse != null) { ParseProperty(propertyToParse, configElement, createdObject, parsedPropertiesDict, context); propertyToParse = parsedPropertiesDict.Keys.FirstOrDefault(k => !parsedPropertiesDict[k]); } return(createdObject); }
public CommandManagerBase(XmlBaseType configurationObject) { }
public object Parse(Type type, XObject config, bool isAssignableTypeAllowed, XmlLocation childLocation, IContext context) { var configElement = config as XElement; if (configElement == null) throw new TestLibsException($"Couldn't parse type: {type} from following config (element node expected):\n{config}"); var xmlType = _xmlType.Value; string uniqueName = null; object createdObject = null; if (context != null) { createdObject = new XmlBaseType(); var xmlNode = _uniqProperty.Value.Location.Value.GetElement(configElement); uniqueName = XmlParser.Parse(_uniqProperty.Value.PropertyType, xmlNode) as string; if (uniqueName != null) { if (context.Contains(type, uniqueName)) return context.ResolveValue(type, uniqueName); } } if (isAssignableTypeAllowed) { var possibleTypeName = configElement.Name.ToString(); var assignableXmlTypes = ReflectionManager.GetAssignableTypes(type); foreach (var assignableXmlType in assignableXmlTypes) { if (assignableXmlType.Location.Check(XmlLocationType.Element, possibleTypeName)) { xmlType = assignableXmlType; } } if (xmlType == _xmlType.Value) throw new TestLibsException($"Couldn't find any type that could be parsed from element with name: {possibleTypeName}\nPossible element names: {assignableXmlTypes.Select(xt => $"Type: {xt.XType}, description: {xt.Description}\n{xt.Location}").ToList().ToStringWithList("=")}"); } else { xmlType = ReflectionManager.GetXmlType(type); } var parsedPropertiesDict = new Dictionary<XmlProperty, bool>(); xmlType.XmlProperties.ForEach(p => parsedPropertiesDict.Add(p, false)); createdObject = Activator.CreateInstance(xmlType.XType); if (uniqueName != null) { _uniqProperty.Value.SetValue(createdObject, uniqueName); parsedPropertiesDict[_uniqProperty.Value] = true; } var propertyToParse = parsedPropertiesDict.Keys.FirstOrDefault(k => !parsedPropertiesDict[k]); while (propertyToParse != null) { ParseProperty(propertyToParse, configElement, createdObject, parsedPropertiesDict, context); propertyToParse = parsedPropertiesDict.Keys.FirstOrDefault(k => !parsedPropertiesDict[k]); } return createdObject; }