我有一个LINQ2SQL生成的类,我想通过got服务公开它。有一些内部属性我不希望它可用。
通常我会在其中抛出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网络服务引用了这一点。
问题是XmlIgnore属性被忽略了,这些属性正在被发送。
有没有人知道这里可能出了什么问题?那么最好的方法是什么呢?
发布于 2009-06-10 13:32:42
AFAIK,XmlSerializer不支持MetadataTypeAttribute (尽管它会很好--我从来没有检查过)。正如您所说,您不能在分部类中添加成员属性。
一种选择可能是将生成的属性设置为非公共属性(private、protected或internal) -并将其命名为类似TimestampStorage (等)的名称-然后在公共API上重新公开它们(在分部类中):
[XmlIgnore]
public object Timestamp {
get {return TimestampStorage; }
set {TimestampStorage = value; }
}
// and other properties(因为XmlSerializer只查看公共API)。这里最大的问题是LINQ-to-SQL查询(Where等)只对生成的列(TimestampStorage等)有效。我以前在成员为internal的情况下使用过这种方法,允许我的DAL类使用internal属性...但这有点含糊其辞。
发布于 2013-11-01 12:21:53
你可以通过将自己的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);发布于 2009-06-10 16:33:38
我同意马克的观点。最简单的做法是将它们标记为内部。您还可以选择使用XmlIgnore在分部类中重新公开它们。顺便说一句,您可以在linq2sql设计器中控制属性的辅助功能。进入属性对话框,您将看到一个设置它们的位置
https://stackoverflow.com/questions/975574
复制相似问题