Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SXmlSerializable shape for fixing Jon Skeet's problems in C# 9.0
/*
The IXmlSerializable interface assumes the mutability of the type the implements it.
Now we have readonly structs, that's not a valid assumption. Ideally, there'd be a static
method that returned a new instance of the type, but you can't have static methods on
interfaces, because reasons.
*/
[Deprecated]
public interface IXmlSerializable
{
void WriteXml(XmlWriter writer);
[Obsolete("This seemed like a good idea in 2002, but it causes problems here in 2022")]
void ReadXml(XmlReader reader);
}
/*
There's an open C# language discussion for something called Shapes, which are like interfaces
but much, much better, and they do allow static methods to be declared, which would allow the
following syntax:
*/
public shape SXmlSerializable<T>
{
void WriteXml(XmlWriter writer);
static T ReadXml(XmlReader reader);
}
// Learn more about Shapes at https://github.com/dotnet/csharplang/issues/164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.