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?
The first thing I’d try is adding a property for serialization, but hiding it from the UI:
You can’t really mix and match serialization unfortunately; once you implement
IXmlSerializable, you own everything. If you were using WCF, thenDataContractSerialiersupports non-public properties for serialization, so you could use:but this doesn’t apply for ‘asmx’ web-services via
XmlSerializer.Does the
[Browsable]trick work at all? Assuming the custom grid usesTypeDescriptor, another option might be to hide it viaICustomTypeDescriptor, but that is a lot of work just to hide a property…