public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { // Normalize value. switch (value) { case int i: value = (long)i; break; } // Convert. switch (value) { case string s: try { return(TokenAmount.Parse(s)); } catch (Exception ex) when(ex is FormatException || ex is OverflowException) { throw new NotSupportedException($"Cannot convert {s} to {typeof(TokenAmount)}.", ex); } case long n: return(new TokenAmount(n)); case decimal d: try { return(TokenAmount.FromDivisible(d)); } catch (ArgumentException ex) { throw new NotSupportedException($"Cannot convert {d} to {typeof(TokenAmount)}.", ex); } default: throw new NotSupportedException( $"Don't know how to convert {value.GetType()} to {typeof(TokenAmount)}."); } }
public static void SerializeTokenAmount(IBufferWriter <byte> writer, TokenAmount value) { BinaryPrimitives.WriteInt64BigEndian(writer.GetSpan(8), value.Value); writer.Advance(8); }