Exemple #1
0
        private static bool TryGetPropertyValue(object formatArgument, 
                                                string argumentName,
                                                string formatOption,
                                                out string argumentValue,
                                                ParseOptions parseOptions)
        {
            argumentValue = null;
            var argumentPath = argumentName.Split('.');

            PropertyInfo property = null;
            //Get nested property value
            foreach (var pathItem in argumentPath)
            {
                var type = formatArgument.GetType();
                property = type.GetProperty(pathItem);
                if (property == null) break;

                formatArgument = property.GetValue(formatArgument, null);
                if (formatArgument == null) break;
            }

            if (property == null) return false;
            if (formatArgument == null) return false;

            if (formatArgument is DateTime)
            {
                //Convert date if necessary
                if (parseOptions.CustomDateConverter != null)
                {
                    formatArgument = parseOptions.CustomDateConverter((DateTime)formatArgument);
                }

                if (!formatOption.IsNullOrEmpty() && allowedDateFormats.Contains(formatOption))
                {
                    argumentValue = ((DateTime)formatArgument).ToString(formatOption);
                }
                else
                {
                    argumentValue = formatArgument.ToString();
                }
            }
            else
            {
                argumentValue = formatArgument.ToString();
            }

            return true;
        }