[英]Conditionally serialize/deserialize attribute
I have a class whose objects must serialize/deserialize an attribute depending on a bool value 我有一个类,其对象必须根据bool值序列化/反序列化属性
[System.SerializableAttribute()]
public class Foo
{
private string myField;
private bool myFieldSerializes;
//Parameterless construction for serializing purposes
public Foo() { }
public Foo(string myField, bool myFieldSerializes)
{
this.myField = myField;
this.myFieldSerializes = myFieldSerializes;
}
public string MyField
{
get {return this.myField;}
set {this.myField = value;}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool MyFieldSerializes
{
get {return this.myFieldSerialzes;}
set {this.myFieldSerialzes = value;}
}
}
This is how it should work: 这是它应该如何工作:
If i create an instance and set myFieldSerializes to 'true', when serializing the object (with XMLSerialize), myField must be serialized (included on the XML message). 如果我创建一个实例并将myFieldSerializes设置为'true',则在序列化对象时(使用XMLSerialize),必须序列化myField(包含在XML消息中)。 If is set to 'false', it should be ignored. 如果设置为'false',则应忽略它。
When deserializing (with XMLDeserialize), the boolean myFieldSerializes should tell me if myField has been deserialized (in other words, it was present in the XML file). 反序列化时(使用XMLDeserialize),布尔值myFieldSerializes应该告诉我myField是否已被反序列化(换句话说,它存在于XML文件中)。
What is the way to implement this behavior? 实现此行为的方法是什么?
Thanks! 谢谢! :) :)
Your requirements match the propertyNameSpecified pattern of XmlSerializer
. 您的要求与XmlSerializer
的propertyNameSpecified模式匹配。 From the docs : 来自文档 :
If a schema includes an element that is optional ... [one] option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. 如果模式包含一个可选的元素...... [one]选项是使用特殊模式创建XmlSerializer识别的布尔字段,并将XmlIgnoreAttribute应用于该字段。 The pattern is created in the form of propertyNameSpecified . 该模式以propertyNameSpecified的形式创建。 For example, if there is a field named "MyFirstName" you would also create a field named "MyFirstNameSpecified" that instructs the XmlSerializer whether to generate the XML element named "MyFirstName". 例如,如果有一个名为“MyFirstName”的字段,您还将创建一个名为“MyFirstNameSpecified”的字段,该字段指示XmlSerializer是否生成名为“MyFirstName”的XML元素。
What's convenient about this pattern is that, beyond the documented behavior, during deserialization, XmlSerializer
will set the propertyNameSpecified to true if the property was encountered -- which is exactly what you need. 这种模式的方便之处在于,除了记录的行为之外,在反序列化期间,如果遇到属性 , XmlSerializer
会将propertyNameSpecified设置为true - 这正是您所需要的。 Thus your class should look like: 因此,您的课程应如下所示:
public class Foo
{
private string myField;
private bool myFieldSerializes;
//Parameterless construction for serializing purposes
public Foo() { }
public Foo(string myField, bool myFieldSerializes)
{
this.myField = myField;
this.myFieldSerializes = myFieldSerializes;
}
[XmlElement(IsNullable = true)] // Emit a value even when null as long as MyFieldSpecified == true
public string MyField
{
get { return this.myField; }
set { this.myField = value; }
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool MyFieldSpecified { get { return myFieldSerializes; } set { myFieldSerializes = value; } }
}
(Adding [XmlElement(IsNullable = true)]
to your MyField
property ensures that an element will always be emitted when MyFieldSpecified == true
, even if the field itself is null
.) (将[XmlElement(IsNullable = true)]
到MyField
属性可确保在MyFieldSpecified == true
时始终发出元素,即使字段本身为null
。)
Seems you can use ShouldSerialize approach: 似乎你可以使用ShouldSerialize方法:
https://msdn.microsoft.com/en-us/library/53b8022e(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/53b8022e(v=vs.110).aspx
public bool ShouldSerializeMyField()
{
return this.myFieldSerializes;
}
You could implement the ISerializable interface which allows you to specify what you want to serialize and deserialize. 您可以实现ISerializable接口,该接口允许您指定要序列化和反序列化的内容。
Therefore you would need to implement an GetObjectData
method like: 因此,您需要实现一个GetObjectData
方法,如:
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Use the AddValue method to specify serialized values.
info.AddValue("props", myProperty_value, typeof(string));
}
and a Constructor which takes SerializationInfo
and StreamingContext
as parameters like 和一个构造函数,它将SerializationInfo
和StreamingContext
作为参数
// The special constructor is used to deserialize values.
public Foo(SerializationInfo info, StreamingContext context)
{
// Reset the property value using the GetValue method.
myProperty_value = (string) info.GetValue("props", typeof(string));
}
In this way you can decide during serialization and deserialization what to do (eg set the value of a propert or not, ...) 通过这种方式,您可以在序列化和反序列化期间决定要做什么(例如,设置属性的值,或者......)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.