예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NamedValue"/> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="value">The value.</param>
 public NamedValue(string name, object value)
 {
     _name            = CheckName(name);
     _value           = value ?? throw new ArgumentNullException(nameof(value));
     _valueType       = _value.GetType();
     _valueTypeHelper = GetValueTypeHelper(_valueType.IsArray ? _valueType.GetElementType().Name : _valueType.Name);
 }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NamedValue"/> class.
        /// </summary>
        /// <param name="rawtext">The text.</param>
        public NamedValue(string rawtext)
        {
            // construct by deserialising text in the format: name/type=value
            string text = rawtext.Trim();

            string[] textParts = text.Split(NameConst.sepPair);
            if (textParts.Length != 2)
            {
                throw new FormatException("Text ('" + text + "') is not in name/type=value format");
            }
            string[] nameParts = textParts[0].Split(NameConst.sepType);
            if (nameParts.Length != 2)
            {
                throw new FormatException("Text ('" + text + "') is not in name/type=value format");
            }
            _name      = CheckName(nameParts[0]);
            _valueType = typeof(string); // default type
            string baseTypeName = nameParts[1];
            bool   isArray      = false;

            if (baseTypeName.EndsWith("[]"))
            {
                isArray      = true;
                baseTypeName = baseTypeName.Remove(baseTypeName.Length - 2);
            }
            if (baseTypeName != "null")
            {
                _valueTypeHelper = GetValueTypeHelper(baseTypeName);
                if (isArray)
                {
                    _valueType = _valueTypeHelper.VectorType;
                    _value     = _valueTypeHelper.DeserialiseVector(DecodeText(textParts[1]));
                }
                else
                {
                    _valueType = _valueTypeHelper.ScalarType;
                    _value     = _valueTypeHelper.DeserialiseScalar(DecodeText(textParts[1]));
                }
            }
        }