Skip to content

Instantly share code, notes, and snippets.

@markrendle
Last active March 2, 2018 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markrendle/74bc21f8e1e3f2ac4b1bdb34da31f205 to your computer and use it in GitHub Desktop.
Save markrendle/74bc21f8e1e3f2ac4b1bdb34da31f205 to your computer and use it in GitHub Desktop.
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