Exemplo n.º 1
0
        /// <summary>
        /// Create dictionary of Key:XmlAttribute specified Name, Value: PropertyInfo
        /// Properties without attributes are skipped
        /// Properties with IsFlattened will have it's own properties expanded <see cref="IsFlattened"/>
        /// </summary>
        /// <param name="type"></param>
        /// <param name="existingDict"></param>
        /// <returns></returns>
        private static Dictionary <string, NestedPropertyInfo> GenXmlNameToPropertyDict(Type type, Dictionary <string, NestedPropertyInfo> existingDict = null, NestedPropertyInfo parentInfo = null)
        {
            var typeCache = TypeResolver.ByType(type);
            var dict      = existingDict ?? new Dictionary <string, NestedPropertyInfo>();
            var members   = typeCache.GetProperties().ToArray();

            //add all properties with custom attribute
            foreach (var m in members)
            {
                var xmlAttr = m.GetCustomAttribute <XmlAttributeAttribute>();
                // skip if has no attribute
                if (xmlAttr == null)
                {
                    continue;
                }
                var prop = new NestedPropertyInfo {
                    ParentInfo = parentInfo, Info = (PropertyInfo)typeCache[m.Name], Attribute = xmlAttr
                };
                if (xmlAttr.IsFlattened)
                {
                    //recursively process sub properties
                    GenXmlNameToPropertyDict(m.PropertyType, existingDict: dict, parentInfo: prop);
                }
                else
                {
                    var completeName = (prop.ParentInfo == null ? "" : prop.ParentInfo.Attribute.Name + "_") + xmlAttr.Name;
                    dict.Add(completeName, prop);
                }
            }
            return(dict);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parse Attribute Argument into the actual string value
        /// </summary>
        /// <param name="propInfo"></param>
        /// <param name="value"></param>
        /// <remarks>
        /// Attribute argument is presented exactly as it was typed
        /// Ex: SomeArg="Test" would result in the Argument.Value "Test" (with quote)
        /// Ex: SomeArg=("Test") would result in the Argument.Value ("Test") (with parentheses and quote)
        /// </remarks>
        public static object ParsePropertyValue(NestedPropertyInfo propInfo, string value)
        {
            object parsed;
            var    propType = propInfo.Info.PropertyType;

            if (propType.IsEnum)
            {
                //if enum, remove the Enum qualifier (e.g TagTypes.InsertPoint becomes InserPoint)
                parsed = Enum.Parse(propType, value.StripQualifier());
            }
            else if (propType == typeof(DateTime) || propType == typeof(DateTime?))
            {
                parsed = DateTime.ParseExact(value, Constants.TagDateFormat, Constants.TagDateCulture);
            }
            else if (propType == typeof(string))
            {
                //remove quotes
                parsed = value.Trim('\"');
            }

            else
            {
                parsed = value;
            }
            return(parsed);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create dictionary of Key:XmlAttribute specified Name, Value: PropertyInfo
        /// Properties without attributes are skipped
        /// Properties with IsFlattened will have it's own properties expanded <see cref="IsFlattened"/>
        /// </summary>
        /// <param name="type"></param>
        /// <param name="existingDict"></param>
        /// <returns></returns>
        private static Dictionary<string, NestedPropertyInfo> GenXmlNameToPropertyDict(Type type,  Dictionary<string, NestedPropertyInfo> existingDict = null, NestedPropertyInfo parentInfo = null)
        {
            var typeCache = TypeResolver.ByType(type);
            var dict = existingDict ?? new Dictionary<string, NestedPropertyInfo>();
            var members = typeCache.GetProperties().ToArray();
            //add all properties with custom attribute
            foreach (var m in members) {
                var xmlAttr = m.GetCustomAttribute<XmlAttributeAttribute>();
                // skip if has no attribute
                if (xmlAttr == null) continue;
                var prop = new NestedPropertyInfo { ParentInfo = parentInfo, Info = (PropertyInfo)typeCache[m.Name], Attribute = xmlAttr };
                if (xmlAttr.IsFlattened)
                {
                    //recursively process sub properties
                    GenXmlNameToPropertyDict(m.PropertyType, existingDict: dict, parentInfo: prop);
                }
                else {

                    var completeName = (prop.ParentInfo== null ? "" : prop.ParentInfo.Attribute.Name + "_") + xmlAttr.Name;
                    dict.Add(completeName, prop);
                }

            }
            return dict;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Parse Attribute Argument into the actual string value
        /// </summary>
        /// <param name="propInfo"></param>
        /// <param name="value"></param>
        /// <remarks>
        /// Attribute argument is presented exactly as it was typed
        /// Ex: SomeArg="Test" would result in the Argument.Value "Test" (with quote)
        /// Ex: SomeArg=("Test") would result in the Argument.Value ("Test") (with parentheses and quote)
        /// </remarks>
        public static object ParsePropertyValue(NestedPropertyInfo propInfo, string value)
        {
            object parsed;
            var propType = propInfo.Info.PropertyType;
            if (propType.IsEnum) {
                //if enum, remove the Enum qualifier (e.g TagTypes.InsertPoint becomes InserPoint)
                parsed = Enum.Parse(propType, value.StripQualifier());
            }
            else if (propType == typeof(DateTime) || propType == typeof(DateTime?)) {
                parsed = DateTime.ParseExact(value, Constants.TagDateFormat, Constants.TagDateCulture);
            }
            else if (propType == typeof(string)) {
                //remove quotes
                parsed = value.Trim('\"');
            }

            else {
                parsed = value;
            }
            return parsed;
        }