I want to serialize class Range<T>
, so I implement ISerializable on it as you see below:
public class Range<T> where T : IComparable<T>, ISerializable
{
/// <summary>Minimum value of the range.</summary>
public T Minimum { get; set; }
/// <summary>Maximum value of the range.</summary>
public T Maximum { get; set; }
/// <summary>Presents the Range in readable format.</summary>
/// <returns>String representation of the Range</returns>
public override string ToString()
{
return string.Format("[{0} - {1}]", Minimum, Maximum);
}
/// <summary>Determines if the range is valid.</summary>
/// <returns>True if range is valid, else false</returns>
public bool IsValid()
{
return Minimum.CompareTo(Maximum) <= 0;
}
public XmlSchema GetSchema() { return null; }
public void ReadXml(XmlReader reader)
{
// start reading
reader.MoveToContent();
Type t = typeof(T);
var min = reader["Minimum"];
var max = reader["Maximum"];
Minimum = (T)Convert.ChangeType(min, t);
Maximum = (T)Convert.ChangeType(max, t);
reader.Read();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteAttributeString("Minimum", Minimum.ToString());
writer.WriteAttributeString("Maximum", Maximum.ToString());
}
}
but when I want to use this class as below ...,
public Range<double> Whr;
I get this error
Severity Code Description Project File Line Suppression State Error CS0315 The type 'double' cannot be used as type parameter 'T' in the generic type or method 'Range'. There is no boxing conversion from 'double' to 'System.Runtime.Serialization.ISerializable'. XCLASS ...\ACLASS.cs 89 Active
How can I fix this error?
I think you want:
public class Range<T> : ISerializable where T : IComparable<T>
The way you have it now, T
must implement ISerializable
. You want the class to implement ISerializable
instead.