Will adding a base class for my objects break .NET/XML deserialization?
The base class will onl开发者_如何学运维y contain methods.
If you don't change the version number of containing assembly then adding base class shouldn't be a problem for binary serialization and for XML it doesn't matter at all since it is very loosly coupled with actual type.
Just make sure that the base is also marked [Serializable] for binary serialization. I've just tested and confirmed this.
Serialized the following class with Binary Formatter
[Serializable]
public class Test
{
public string Name { get; set; }
}
Got the following base64 encoded output:
AAEAAAD/////AQAAAAAAAAAMAgAAAEpDb25zb2xlQXBwbGljYXRpb24xLCBWZXJzaW9uPTEuMC4wLjAs IEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbAUBAAAAIENvbnNvbGVBcHBsaWNhdGlv bjEuUHJvZ3JhbStUZXN0AQAAABU8TmFtZT5rX19CYWNraW5nRmllbGQBAgAAAAYDAAAABWhhc2FuCw==
Then added following base class for Test:
[Serializable]
public class Base
{
public void Dhuz() { }
}
And de serialized the base64 encoded thing again successfully without problems.
精彩评论