Exemplo n.º 1
0
        public XmlLocation(XmlProperty propertyInfo, bool isChildLocation = false)
        {
            if (!isChildLocation)
            {
                var locationAtts = propertyInfo.Info.GetCustomAttributes <XmlLocationAttribute>(true).ToList();
                locationAtts.Add(new XmlLocationAttribute(XmlLocationType.Element | XmlLocationType.Attribute, propertyInfo.Info.Name));
                locationAtts.ForEach(AddLocation);

                if (propertyInfo.IsAssignableTypesAllowed)
                {
                    var assignableTypes = ReflectionManager.GetAssignableTypes(propertyInfo.PropertyType);
                    assignableTypes.ForEach(AddLocation);
                }
                else
                {
                    AddLocation(propertyInfo.PropertyType);
                }
            }
            else
            {
                var childLocationAtts = propertyInfo.Info.GetCustomAttributes <XmlChildLocationAttribute>(true).ToList();
                childLocationAtts.ForEach(AddLocation);

                var childType = propertyInfo.PropertyType.IsGenericType
                        ? propertyInfo.PropertyType.GetGenericArguments().Last()
                        : (propertyInfo.PropertyType.IsArray
                            ? propertyInfo.PropertyType.GetElementType()
                            : propertyInfo.PropertyType);

                if (childType == null)
                {
                    throw new TestLibsException($"Couldn't find child type for type: {propertyInfo.PropertyType}, for property: {propertyInfo}");
                }

                if (propertyInfo.IsAssignableTypesAllowed)
                {
                    var assignableTypes = ReflectionManager.GetAssignableTypes(childType);
                    assignableTypes.ForEach(AddLocation);
                }
                else
                {
                    AddLocation(childType);
                }
            }
        }
Exemplo n.º 2
0
        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);
        }