从 'double' 到 'System.Runtime.Serialization.ISerializable' 的装箱转换
Boxing conversion from 'double' to 'System.Runtime.Serialization.ISerializable'
我想序列化 class Range<T>
,所以我在上面实现了 ISerializable,如下所示:
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());
}
}
但是当我想使用这个 class 如下...,
public Range<double> Whr;
我收到这个错误
严重性代码说明项目文件行抑制状态
错误 CS0315 类型 'double' 不能用作泛型类型或方法 'Range' 中的类型参数 'T'。没有从 'double' 到 'System.Runtime.Serialization.ISerializable' 的装箱转换。 XCLASS ...\ACLASS.cs 89 活动
我该如何解决这个错误?
我想你想要:
public class Range<T> : ISerializable where T : IComparable<T>
按照您现在的方式,T
必须实施 ISerializable
。您希望 class 改为实施 ISerializable
。
我想序列化 class Range<T>
,所以我在上面实现了 ISerializable,如下所示:
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());
}
}
但是当我想使用这个 class 如下...,
public Range<double> Whr;
我收到这个错误
严重性代码说明项目文件行抑制状态 错误 CS0315 类型 'double' 不能用作泛型类型或方法 'Range' 中的类型参数 'T'。没有从 'double' 到 'System.Runtime.Serialization.ISerializable' 的装箱转换。 XCLASS ...\ACLASS.cs 89 活动
我该如何解决这个错误?
我想你想要:
public class Range<T> : ISerializable where T : IComparable<T>
按照您现在的方式,T
必须实施 ISerializable
。您希望 class 改为实施 ISerializable
。