SettingsBase
is the type which is used as WinForms settings.
Let's say I have a basic type:
[Serializable]
public sealed class DeviceConfiguration
{
public string DeviceName;
public DeviceDetails Details;
}
I can use this type in my settings (i.e. it is serialized and saved correctly). But let me be polymorphic:
[Serializable]
public sealed class DeviceConfiguration
{
public string DeviceName;
public IDeviceDetails Details; // interface (or abstract type: see update)
}
Such type is not serialized, and it is not saved (via SettingsBase
, in my case as WinForms settings).
I tried several tricks, deriving IDeviceDetails
from ISerializable
, marking this field as non-serializable (just a sanity check), deriving this type (DeviceConfiguration
) from ISerializable
and providing custom methods for serialization. Nay-nay, interface on board -- it won't be serialized.
So is there some rule that says indeed "field with interface type makes entire type non-serializable"? Or this is some nasty gotcha? Or I miss some crucial detail?
I didn't know that interface cannot be serialized -- ISerializable is an interface -- but anyway, it does not explain this story -- here serialization of entire type failed even when interface field was ommitted (custom serialization skipped that field, and NonSerializable
also should be noticed).
Update: I turned the interface into an abstract type (class), so no more interfaces at all, still serialization fails.