views:

530

answers:

1

Hi,

I've got a class with well over 100 properties (it's a database mapping class) and one of the properties has to be in a method. In other words this data is not exposed via a property but via methods:

"ABCType GetABC(), SetABC(ABCType value)"

It's all very un-C#-like. I shudder when I see it.

The class needs to be serializable so it can be sent over web services, and the data exposed by the Get/Set methods needs to be serialized too. (It's in a method because of a strange thing the grid I'm using does with reflection; it can't handle objects that contain properties of the same type as the containing object. The problem property stores the original state of the database object in case a revert is required. Inefficient implementation, yes - but I'm unable to re-engineer it.)

My question is this: since only this 1 field needs custom serialization code, I'd like to use custom serialization only for calling GetABC and SetABC, reverting to basic XML serialization for the rest of the class. It'll minimize potential for bugs in my serialization code. Is there a way?

+1  A: 

The first thing I'd try is adding a property for serialization, but hiding it from the UI:

[Browsable(false)] // hide in UI
public SomeType ABC {
    get {return GetABC();}
    set {SetABC(value);}
}

You can't really mix and match serialization unfortunately; once you implement IXmlSerializable, you own everything. If you were using WCF, then DataContractSerialier supports non-public properties for serialization, so you could use:

[DataMember]
private SomeType ABC {
    get {return GetABC();}
    set {SetABC(value);}
}

but this doesn't apply for "asmx" web-services via XmlSerializer.

Does the [Browsable] trick work at all? Assuming the custom grid uses TypeDescriptor, another option might be to hide it via ICustomTypeDescriptor, but that is a lot of work just to hide a property...

Marc Gravell
The [Browsable] trick doesn't work because it won't hide the property from the grid's call to typeof(SomeType).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); However you did answer the question about mixed serialization, so thanks!