我有一个 LINQ 2 SQL 生成的类,我想通过网络服务公开。 有一些我不想使用的内部属性。
通常我会在其中放入 [XmlIgnore],但因为属性在生成的一半中,所以我不能这样做。
我一直在考虑在 this post 之后使用 MetadataType这看起来应该允许我在另一个类中定义属性。
我的代码看起来像这样:
[MetadataType(typeof(ProspectMetaData))]
public partial class Prospect : ApplicationBaseObject
{
}
public class ProspectMetaData
{
[XmlIgnore]
public object CreatedDateTime { get; set; }
[XmlIgnore]
public object AmendedDateTime { get; set; }
[XmlIgnore]
public object Timestamp { get; set; }
}
我通过 Silverlight 项目中的 ASP.NET Web 服务引用它。
问题是 [XmlIgnore] 属性被忽略,这些属性正在被发送。
有没有人知道这里可能出了什么问题?最好的方法是什么?
最佳答案
您可以通过将自己的 XmlAttributeOverrides 传递给 XmlSerializer 来完成此操作,在 XmlAttributeOverrides 中,您可以将 XmlIgnore 更改为您希望的字段/属性的 true,而无需触及 DBML 生成的代码,它就像一个魅力,使用相同的覆盖反序列化......
Refer this link了解更多信息
// Create the XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
attrs.XmlIgnore = true;
/* Setting XmlIgnore to true overrides the XmlIgnoreAttribute
applied to the following fields. Thus it will be serialized.*/
xOver.Add(typeof(Prospect), "CreatedDateTime", attrs);
xOver.Add(typeof(Prospect), "AmendedDateTime", attrs);
xOver.Add(typeof(Prospect), "Timestamp", attrs);
XmlSerializer xSer = new XmlSerializer(typeof(Prospect), xOver);
TextWriter writer = new StreamWriter(outputFilePath);
xSer.Serialize(writer, object);
关于c# - 在生成的部分类上使用 XmlIgnore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/975574/