Both the BinaryFormatter and SoapFormatter fails when deserializing
decimals with a value between 0 an 1E-4. The problem seems to be that the
small decimals are serialized in scientific notation, which Decimal.Parse
() won't accept.
This must be a bug. Does any of you know how to get arround it?
I could write my own decimal struct, but it would be hard to enforce using
this new struct in our code. I could also write my own serializer, but
that seems to be a lot of work. I would rather intercept either the
serialization or deserialization of decimals. Is that even possible?
TIA S�ren
The c# code below demonstrates the bug:
------------------------------------------
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication1
{
[Serializable]
public struct Temp
{
public decimal num1;
}
class Class1
{
static void Main(string[] args)
{
Temp x = new Temp();
x.num1 = 0.00001M; // Is serialized to "1E-05" which
// fails on deserialization.
//x.num1 = 0.0001M; // OK. Serialized to "0.0001".
Stream s1 = File.Open("C:\\Temp.bin", FileMode.Create);
BinaryFormatter f1 = new BinaryFormatter();
f1.Serialize(s1, x);
s1.Close();
Stream s2 = File.Open("C:\\Temp.bin", FileMode.Open);
BinaryFormatter f2 = new BinaryFormatter();
Temp y = (Temp)f2.Deserialize(s2);
s2.Close();
}
}
}
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.