GetScatter / eos-sharp

C# client library for EOS blockchains
MIT License
75 stars 48 forks source link

object to float conversion InvalidCastException #41

Closed KGMaxey closed 4 years ago

KGMaxey commented 4 years ago

https://github.com/GetScatter/eos-sharp/blob/bd4b875a794222328bf5b91d3bc8fd1adff6ffa7/EosSharp/EosSharp.Core/Providers/AbiSerializationProvider.cs#L409

When casting an object to a float, using parenthesis casting causes an InvalidCastException. I only just started reading a few of the articles linked below to catch myself up with this issue and learn more about how casting works in C#, but our testing shows that changing the code to use the conversion tool handles this just fine.

https://social.msdn.microsoft.com/Forums/en-US/4b48ac71-2094-42ac-88de-60004048cb11/object-to-float-casting-problem?forum=csharplanguage

http://www.interact-sw.co.uk/iangblog/2004/01/20/casting

 private static void WriteFloat32(MemoryStream ms, object value)
 {
    // ms.Write(BitConverter.GetBytes((float)value), 0, 4);
    ms.Write(BitConverter.GetBytes(Convert.ToSingle(value), 0, 4);
 }

Currently we're running in Unity and just pulled down this repository straight into our project out of simplicity so we are able to use this work around now. If there's anything we're missing please let us know and we appreciate your help!

mmcs85 commented 4 years ago

This makes sense, I will make the change.

mmcs85 commented 4 years ago

Made fix here: https://github.com/GetScatter/eos-sharp/commit/a17ed0349082c92d94871c8cd7088b0f725595f5