views:

61

answers:

2

Is it normal practice to use a TypeConverter for serialization? There is a class that I do not own that has a "lossy" TypeConverter. When converting to a string, it formats its floating point data with "G4", so that when this type is displayed in a PropertyGrid, it's easily readable.

I would like to also use this TypeConverter to convert from a string, creating an instance of this class. Right now I'm checking the CultureInfo passed to TypeConverter.ConvertTo and only using the pretty, lossy conversion if the CultureInfo is not InvariantCulture.

I'd like to know if I'm going about this the wrong way.

+1  A: 

If you are serializing data into a file or other interchange format to be shared between users in different cultures, using anything but InvariantCulture won't work.

TypeConverter can be used in simple serialization scenarios, when all needed types are known to have an appropriate TypeConverter.

bbudge
Thank you. Is the current use of the TypeConverter, for lossy formatted output to a ProperyGrid, also normal practice?
Cat
+1  A: 

Well, it isn't normal practice. You'd want some kind of control over how the object gets serialized so that it doesn't trip you up with details that are only relevant to a PropertyGrid. That's not usually hard to do:

class VendorSerialized {
    public VendorSerialized(VendorType obj) {
        // Set properties
        //...
    }
    public VendorType AfterSerialization() {
        var obj = new VendorType();
        // Set the vendor object properties from deserialized data
        //...
        return obj;
    }
    // Properties here...
    //...
}

Problem solved :)

Hans Passant
I don't see how Culture-specific strings will ever end up as the input to deserialization. Am I missing something?
Cat
Erm, why would you want the deserialization to break when the culture changes? Localization is a UI detail, it should never affect persisted data.
Hans Passant
"You'd want some kind of control over how the object gets serialized so that it doesn't trip you up with details that are only relevant to a PropertyGrid."I have control in the TypeConverter.ConvertToString, don't I? I have the CultureInfo I need in order to make a decision as to whether I'm spitting out a prettified string, or a lossless representation.I think I don't fully understand your response.
Cat