Ejemplo n.º 1
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            // Normalize value.
            switch (value)
            {
            case int i:
                value = (long)i;
                break;
            }

            // Convert.
            if (value is string s)
            {
                try
                {
                    return(PropertyAmount.Parse(s));
                }
                catch (Exception ex) when(ex is FormatException || ex is OverflowException)
                {
                    throw new NotSupportedException($"Cannot convert {s} to {typeof(PropertyAmount)}.", ex);
                }
            }
            else if (value is long n)
            {
                return(new PropertyAmount(n));
            }
            else if (value is decimal d)
            {
                try
                {
                    return(PropertyAmount.FromDivisible(d));
                }
                catch (ArgumentException ex)
                {
                    throw new NotSupportedException($"Cannot convert {d} to {typeof(PropertyAmount)}.", ex);
                }
            }
            else
            {
                throw new NotSupportedException(
                          $"Don't know how to convert {value.GetType()} to {typeof(PropertyAmount)}."
                          );
            }
        }
Ejemplo n.º 2
0
        public SimpleSendV0(BitcoinAddress sender, BitcoinAddress receiver, PropertyId property, PropertyAmount amount)
            : base(sender, receiver)
        {
            if (sender == null)
            {
                throw new ArgumentNullException(nameof(sender));
            }

            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            if (amount <= PropertyAmount.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), amount, "The value is less than one.");
            }

            Property = property;
            Amount   = amount;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Write a <see cref="PropertyAmount"/> to <paramref name="writer"/>.
        /// </summary>
        protected static void EncodePropertyAmount(BinaryWriter writer, PropertyAmount amount)
        {
            var value = IPAddress.HostToNetworkOrder(amount.Indivisible);

            writer.Write(value);
        }