| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System;
- namespace Quadarax.Foundation.Core.QConsole.Value
- {
- public abstract class AbstractValue
- {
- private object _value;
- /// <summary>
- /// Defines if value is set. Not null.
- /// </summary>
- public bool HasValue => _value != null;
- /// <summary>
- /// Returns underlying system value type.
- /// </summary>
- public Type ValueType { get; }
- public AbstractValue()
- {
- Reset();
- }
- public AbstractValue(Type valueType, object value) : this(valueType)
- {
- _value = value;
- }
- protected AbstractValue(Type valueType)
- {
- ValueType = valueType ?? throw new ArgumentNullException(nameof(valueType));
- }
- protected abstract object ConvertStringToValue(string valueAsString);
- public abstract string AsString();
- public void Set(string valueAsString)
- {
- if (string.IsNullOrEmpty(valueAsString))
- {
- Reset();
- return;
- }
- Set(ConvertStringToValue(valueAsString));
- }
- public virtual void Set(object value)
- {
- _value = value;
- }
- public object Get()
- {
- return _value;
- }
- public void Reset()
- {
- _value = null;
- }
- public static AbstractValue Create(TypeValuesEnum typeValue)
- {
- switch (typeValue)
- {
- case TypeValuesEnum.Bool:
- return new BoolValue();
- case TypeValuesEnum.Date:
- return new DateValue();
- case TypeValuesEnum.DateTime:
- return new DateTimeValue();
- case TypeValuesEnum.Decimal:
- return new DecimalValue();
- case TypeValuesEnum.Guid:
- return new GuidValue();
- case TypeValuesEnum.Integer:
- return new IntegerValue();
- case TypeValuesEnum.String:
- return new StringValue();
- case TypeValuesEnum.Time:
- return new TimeValue();
- case TypeValuesEnum.TimeSpan:
- return new TimeSpanValue();
- default:
- throw new NotSupportedException($"Unsupported argument value '{typeValue}'");
- }
- }
- public override string ToString()
- {
- return AsString();
- }
- }
- }
|