Ejemplo n.º 1
0
        private object ReadEnumValue(string enumText, Type enumType)
        {
            object result = null;

            if (!UseDefaultEnumValueIfUnknown || Enum.GetNames(enumType).Any(r => r == enumText))
            {
                result = CustomConvert.ChangeType(enumText, enumType, CultureInfo.InvariantCulture);
            }
            if (result == null)
            {
                result = CustomConvert.ChangeType(0, enumType, CultureInfo.InvariantCulture);
            }

            return(result);
        }
Ejemplo n.º 2
0
        private object ReadValue(XElement node, string name, Type type, bool isListElement = false)
        {
            object result = null;

            name = name ?? GetValueName(type, lowerCamelCaseValueTypeName: isListElement);
            if (name == node.Name)
            {
                var nilAttribute = node.Attribute(NilAttributeName);
                var isNil        = nilAttribute != null && nilAttribute.Value == "true";
                if (!isNil)
                {
                    var isString = type == typeof(String);
                    var isList   = typeof(IList).IsAssignableFrom(type);
                    var isClass  = !type.IsValueType && !isString && !isList;

                    if (isClass)
                    {
                        result = Activator.CreateInstance(type);
                        ReadProperties(node, result);
                    }
                    else if (isList)
                    {
                        result = Activator.CreateInstance(type);
                        ReadList(node, (IList)result);
                    }
                    else if (type.IsEnum)
                    {
                        result = ReadEnumValue(node.Value, type);
                    }
                    else
                    {
                        var valueText     = node.Value;
                        var isChar        = type == typeof(char); // XmlSerializer writes chars as ints...
                        var convertToType = isChar ? typeof(int) : type;
                        result = CustomConvert.ChangeType(valueText, convertToType, CultureInfo.InvariantCulture);
                        if (isChar)
                        {
                            result = (char)(int)result;
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses a text value into an object of the type required by the parameter.
        /// </summary>
        /// <param name="parameterInfo"></param>
        /// <param name="textValue"></param>
        /// <returns></returns>
        private object ParseTextValue(ParameterInfo parameterInfo, string textValue)
        {
            object result = null;

            if (parameterInfo.ParameterType == typeof(string))
            {
                result = textValue;
            }
            else if (parameterInfo.ParameterType.IsClass)
            {
                if (!String.IsNullOrEmpty(textValue))
                {
                    result = JsonConvert.DeserializeObject(textValue, parameterInfo.ParameterType);
                }
            }
            else
            {
                result = CustomConvert.ChangeType(textValue, parameterInfo.ParameterType, CultureInfo.InvariantCulture);
            }

            return(result);
        }