I've got a class with a System.Version
property, which looks like this:
- Version
- Build: 111
- Major: 1
- MajorRevision: 0
- Minor: 1
- MinorRevision: 10
- Revision: 10
When I serialize the class, version is always empty:
<Version />
The Client class looks like:
[Serializable]
public class Client
{
public string Description;
public string Directory;
public DateTime ReleaseDate;
public Version Version;
}
You can use string proxy property:
You need to define your
get
andset
accessors, as:I got this output:
I have cleaned up Nick Craver's implementation to be much more concise and readable, added XML Serialization support (with attributes instead of separate elements), implemented a string TypeConverter as well as fixed a couple comparison issues that had the possibility for NullReferenceExceptions or infinite loops (calling the overloaded operators from within themselves).
I also used some C# 6 features as well.
I prefer to use approach below, so I have only one property with Type VersionXml in my class. Implicit operators are really useful here.
System.Version
is not serializable, if you look at it's properties on MSDN, you'll see they have no setters...so the serializer won't store them. However, this approach still works. That article (old but still works) provides a Version class that is serializable, can you switch to that and get going?Edit by tomfanning
I have fished the code from the dead site out of archive.org, reproduced below.
More precisely,
System.Version
is not XML-serializable. It is serializable with aBinaryFormatter
, for example: