在部分类实体框架的String属性是没有序列号 [英] String property in partial class of the entity framework is not serialized
问题描述
我想序列化部分类实体框架的一个字符串属性。
I'm trying to serialize a string property in partial class of the entity framework.
public partial class tableTest : EntityObject
{
public String testA
{
get
{
return "ok";
}
}
[XmlElement("TestB")]
public List<String> TestB
{
get { var list = new List<String>(); list.Add("testB"); return list; }
}
}
属性外种皮不工作,但物业TESTB作品。
我试图添加的XmlElement,XmlAttribute,DataMemberAttribute ...的种皮,没有什么工作。
Property TestA is not working, but property TestB works. I tried to add XmlElement, XmlAttribute, DataMemberAttribute... to the TestA, nothing works.
任何人有一个猜测?
(.NET框架4.5)
(.net framework 4.5)
推荐答案
的 [的XmlElement]
建议您使用的XmlSerializer
;现在,如果我们考虑:
The [XmlElement]
suggests you are using XmlSerializer
; now, if we consider:
public String testA
{
get
{
return "ok";
}
}
注意的XmlSerializer
会忽略这一点,因为它知道它的不能反序列化的(有没有setter)。序列化器希望还能够的反序列化的事后数据。它不序列它,因为它不能没有制定者提供反序列化。
Note that XmlSerializer
will ignore this because it knows it can't deserialize it (there is no setter). A serializer wants to also be able to deserialize the data afterwards. It doesn't serialize it because it can't offer deserialization without a setter.
添加一个setter。理想情况下,例如:
Add a setter. Ideally, for example:
[XmlElement("testA")]
public string TestA {get;set;}
(并给它的价值确定
另发)
另请注意,您的列表不会在此刻无论是正常反序列化;我建议:
Note also that your list won't deserialize properly at the moment either; I would suggest:
private readonly List<string> testB = new List<string>();
[XmlElement("TestB")]
public List<string> TestB { get { return testB; } }
(并再次 - 添加测试数据分开)
(and again - add the test data separately)
这篇关于在部分类实体框架的String属性是没有序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!