1

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.

astrowalker
  • 3,123
  • 3
  • 21
  • 40
  • 1
    Possible duplicate of [Serializing interfaces](https://stackoverflow.com/questions/4659248/serializing-interfaces) – David Ferenczy Rogožan Sep 25 '18 at 10:43
  • @DawidFerenczy, thank you, but it does not explain why serialization fails when interface is not serialized at all (custom serialization skipped that field, and `NonSerializable` also should be noticed). – astrowalker Sep 25 '18 at 10:51

0 Answers0