C#类和结构
我对C中的序列化有一些问题# 拥有代码:C#类和结构,c#,class,struct,serializable,C#,Class,Struct,Serializable,我对C中的序列化有一些问题# 拥有代码: public struct CoOrds { public double x, y, z; public CoOrds(double p1, double p2, double p3) { x = p1; y = p2; z = p3; } } public struct printColor { public int r, g, b; public pr
public struct CoOrds
{
public double x, y, z;
public CoOrds(double p1, double p2, double p3)
{
x = p1;
y = p2;
z = p3;
}
}
public struct printColor
{
public int r, g, b;
public printColor(int p1, int p2, int p3)
{
r = p1;
g = p2;
b = p3;
}
}
[Serializable]
public abstract class shape : ISerializable
{
public int borderStyle=1;
/* ===============================COLOR PARAMETERS================================ */
public printColor colorRGB = new printColor(0, 0, 0);
public System.Drawing.Drawing2D.DashStyle styleLine { get; set; }
public int widht=2;
/*=================================FILL PARAMETERS=====================================*/
public printColor fillColorRGB = new printColor(255,255, 255);
public shape()
{
}
protected shape(SerializationInfo info, StreamingContext context)
{
colorRGB.r = info.GetInt32("colorLine.r");
colorRGB.g = info.GetInt32("colorLine.g");
colorRGB.b = info.GetInt32("colorLine.b");
borderStyle = info.GetInt32("borderStyle");
fillColorRGB.r = info.GetInt32("fillColorRGB.r");
fillColorRGB.g = info.GetInt32("fillColorRGB.g");
fillColorRGB.b = info.GetInt32("fillColorRGB.b");
widht = info.GetInt32("widht");
}
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("colorLine.r",colorRGB.r);
info.AddValue("colorLine.g", colorRGB.g);
info.AddValue("colorLine.b", colorRGB.b);
info.AddValue("borderStyle",borderStyle);
info.AddValue("fillColorRGB.r", fillColorRGB.r);
info.AddValue("fillColorRGB.g", fillColorRGB.g);
info.AddValue("fillColorRGB.b", fillColorRGB.b);
info.AddValue("widht", widht);
}
.....
一,。我怎样才能像序列化结构一样
[Serializable]
public struct CoOrds
在类下以及如何将其放入形状和GetObjectData中,因为我有太多的类必须使用此结构
Thx您可以直接使用
info.AddValue(“Pos”,Pos,typeof(CoOrds)
对其进行序列化,或者您可以将其转换为string
[Serializable()]
public struct Coords
{
readonly public double x, y, z;
public Coords(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static Coords FromString(string value)
{
if (string.IsNullOrEmpty(value)) return new Coords();
double x = 0,y= 0,z = 0;
string[] parts = value.Split(',');
if (parts.Length > 0) double.TryParse(parts[0], out x);
if (parts.Length > 1) double.TryParse(parts[1], out y);
if (parts.Length > 2) double.TryParse(parts[2], out z);
return new Coords(x, y, z);
}
public override string ToString()
{
//Ensure round-trip formatting
return string.Format("{0:R},{1:R},{2:R}", x, y, z);
}
}
然后用
[Serializable()]
public class Vertex : ISerializable
{
public Coords pos1, pos2;
...
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Pos1", pos1.ToString());
info.AddValue("Pos2", pos2, typeof(Coords));
}
public Vertex(SerializationInfo info, StreamingContext context)
{
this.pos1 = Coords.FromString(info.GetValue("Pos1", typeof(string)) as string);
this.pos2 = (Coords)info.GetValue("Pos2", typeof(Coords));
}
}
与我所做的测试代码一样好。您可以直接使用
info.AddValue(“Pos”,Pos,typeof(CoOrds)
对其进行序列化,或者您可以将其转换为/从字符串
[Serializable()]
public struct Coords
{
readonly public double x, y, z;
public Coords(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static Coords FromString(string value)
{
if (string.IsNullOrEmpty(value)) return new Coords();
double x = 0,y= 0,z = 0;
string[] parts = value.Split(',');
if (parts.Length > 0) double.TryParse(parts[0], out x);
if (parts.Length > 1) double.TryParse(parts[1], out y);
if (parts.Length > 2) double.TryParse(parts[2], out z);
return new Coords(x, y, z);
}
public override string ToString()
{
//Ensure round-trip formatting
return string.Format("{0:R},{1:R},{2:R}", x, y, z);
}
}
然后用
[Serializable()]
public class Vertex : ISerializable
{
public Coords pos1, pos2;
...
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Pos1", pos1.ToString());
info.AddValue("Pos2", pos2, typeof(Coords));
}
public Vertex(SerializationInfo info, StreamingContext context)
{
this.pos1 = Coords.FromString(info.GetValue("Pos1", typeof(string)) as string);
this.pos2 = (Coords)info.GetValue("Pos2", typeof(Coords));
}
}
与测试代码一样,我做得很好。@user1137147:我真的不理解你的问题。如果你将结构的内容保存在二进制序列化中,比如你使用的
colorline.R
?现在我想知道关于可序列化的问题,我现在可以这样做,但我必须这样做的代码太多了s可序列化,如果我能做得更简单,我会做的。可变结构是邪恶的。除非绝对必要,否则尽量避免使用它们。是否有什么原因必须实现ISerializable
?是否将struct
标记为[serializable]
还是不?@user1137147:我真的不明白你的问题。如果你把结构的内容保存在二进制序列化中,比如你用colorline.R
?现在我只想知道可序列化的问题。我现在可以这样做,但是我有太多的代码必须进行序列化,如果我可以轻松地完成的话我不知道,可变结构是邪恶的。除非绝对必要,否则尽量避免使用它们。你有什么理由必须实现ISerializable
?struct
是否标记为[Serializable]
?ooooooo!这就是我搜索的,thxooo!这就是我搜索的,Thx