In C#, what's the safe way to do generically convert a primitive to a string for serialization? -
i'd generically serialize class/struct in c#. sake of simplicity, let's assume class/struct can 1 level deep (no structs of structs).
here's i'd write
system.text.stringbuilder builder = new system.text.stringbuilder(); system.reflection.fieldinfo[] fields = obj.gettype().getfields(); foreach (system.reflection.fieldinfo info in fields) { object fieldvalue = info.getvalue(obj); if (fieldvalue != null) { builder.append(fieldvalue.tostring()); } ... }
unfortunately far can tell won't work because tostring()
culture sensitive. in other words single
might output 12.345
or 12,345
depending on culture settings.
is there other non-culture sensitive tostring
can call primitive types?
also, there non-culture sensitive generic string object function. i'm using
object value = system.convert.changetype(string, sometype);
but that's apparently culture sensitive. :(
for changetype, there's overload accepts iformatprovider can call:
system.convert.changetype(string, sometype, cultureinfo.invariantculture);
Comments
Post a Comment