我有一个 LINQ to SQL 类 (dbml),表示数据库中的一些表。我通过使用新属性 (DwellingCode) 扩展其中一个表 (Dwelling) 的部分类来添加各种“计算字段”;此属性采用住宅表中几个数据库字段的值并生成一个字符串。
到目前为止,一切正常;我可以让这个计算字段在网页等的绑定(bind)控件中正确显示。问题是我还有一个返回 Dwelling 对象的 SOAP Web 服务。当 Dwelling 对象序列化为 XML 时,DwellingCode 不包含在“真实”数据库字段的其余部分中。
我需要做什么才能使 DwellingCode 属性与其余数据库字段序列化?基于一些谷歌搜索,我尝试将 [DataMember] 和 [DataMamberAttribute] 添加到 DwellingCode 属性,并将 [DataContract] 和 [Serializable] 添加到部分类,但似乎没有任何效果。
public partial class Dwelling
{
public string DwellingCode
{
get
{
// code to concatenate fields here
}
}
}
最佳答案
好的,将此添加到 DwellingCode 属性有效:
[global::System.Runtime.Serialization.DataMemberAttribute(Name = "DwellingCode")]
出于某种原因,这些不起作用。我不确定为什么在我“使用”它时删除完整命名空间路径不起作用,而且我读过的文档说如果未指定名称参数,名称将与属性名称相同,所以我不知道它为什么这么挑剔。
using System.Runtime.Serialization;
...
[DataMemberAttribute(Name = "DwellingCode")]
[DataMemberAttribute()]
编辑:
我还需要添加一个空白 setter,否则该属性不会显示。随便。
[global::System.Runtime.Serialization.DataMemberAttribute(Name = "DwellingCode")]
public string DwellingCode
{
set
{
}
get
{
// code goes here
}
}
关于c# - 获取要序列化的扩展 LINQ 类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3418234/