Exemplo n.º 1
0
        /// <summary>
        /// Tries to parse the string into an object of the type this instance represents.
        /// Returns false when no suitable TryParse methods exists for the type or when parsing fails
        /// for any reason. When possible, this method uses CultureInfo.InvariantCulture and NumberStyles.Any.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <param name="result">The result.</param>
        /// <returns><c>true</c> if parse was converted successfully; otherwise, <c>false</c>.</returns>
        public bool TryParse(string s, out object result)
        {
            result = GetDefault();

            try
            {
                if (Type == typeof(string))
                {
                    result = Convert.ChangeType(s, Type);
                    return(true);
                }

                if ((IsNullableValueType && string.IsNullOrEmpty(s)) || !CanParseNatively)
                {
                    return(true);
                }

                // Build the arguments of the TryParse method
                var dynamicArguments = new List <object> {
                    s
                };

                for (var pi = 1; pi < _tryParseParameters.Length - 1; pi++)
                {
                    var argInfo = _tryParseParameters[pi];
                    if (argInfo.ParameterType == typeof(IFormatProvider))
                    {
                        dynamicArguments.Add(CultureInfo.InvariantCulture);
                    }
                    else if (argInfo.ParameterType == typeof(NumberStyles))
                    {
                        dynamicArguments.Add(NumberStyles.Any);
                    }
                    else
                    {
                        dynamicArguments.Add(null);
                    }
                }

                dynamicArguments.Add(null);
                var parseArguments = dynamicArguments.ToArray();

                if ((bool)TryParseMethodInfo.Invoke(null, parseArguments))
                {
                    result = parseArguments[parseArguments.Length - 1];
                    return(true);
                }
            }
            catch
            {
                // Ignore
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExtendedTypeInfo"/> class.
        /// </summary>
        /// <param name="t">The t.</param>
        public ExtendedTypeInfo(Type t)
        {
            Type = t ?? throw new ArgumentNullException(nameof(t));
            IsNullableValueType = Type.GetTypeInfo().IsGenericType &&
                                  Type.GetGenericTypeDefinition() == typeof(Nullable <>);

            IsValueType = t.GetTypeInfo().IsValueType;

            UnderlyingType = IsNullableValueType ?
                             new NullableConverter(Type).UnderlyingType :
                             Type;

            IsNumeric = NumericTypes.Contains(UnderlyingType);

            // Extract the TryParse method info
            try
            {
                TryParseMethodInfo = UnderlyingType.GetMethod(TryParseMethodName,
                                                              new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), UnderlyingType.MakeByRefType() }) ??
                                     UnderlyingType.GetMethod(TryParseMethodName,
                                                              new[] { typeof(string), UnderlyingType.MakeByRefType() });

                _tryParseParameters = TryParseMethodInfo?.GetParameters();
            }
            catch
            {
                // ignored
            }

            // Extract the ToString method Info
            try
            {
                ToStringMethodInfo = UnderlyingType.GetMethod(ToStringMethodName,
                                                              new[] { typeof(IFormatProvider) }) ??
                                     UnderlyingType.GetMethod(ToStringMethodName,
                                                              new Type[] { });

                _toStringArgumentLength = ToStringMethodInfo?.GetParameters().Length ?? 0;
            }
            catch
            {
                // ignored
            }
        }