Example #1
0
        /// <summary>
        /// Sets the value of this member from a string.
        /// </summary>
        /// <param name="item">The instance to set this member value.</param>
        /// <param name="s">The string value to set.</param>
        /// <param name="throwExceptionOnInvalidData">If true, a <see cref="FixedWidthDataException"/>
        /// is thrown if <paramref name="s"/> could not be converted to this member type.</param>
        /// <exception cref="FixedWidthDataException"></exception>
        public void SetValue(object item, string s, bool throwExceptionOnInvalidData)
        {
            Debug.Assert(Member != null);
            Debug.Assert(Converter != null);

            if (Member.CanWrite)
            {
                if (Converter.TryConvertFromString(s, out object?value))
                {
                    Member.SetValue(item, value);
                }
                else
                {
                    // Could not convert field
                    if (throwExceptionOnInvalidData)
                    {
                        throw new FixedWidthDataException(Member.Name, s, Member.Type.Name);
                    }
                }
            }
        }