上产生局部类使用XmlIgnore [英] Using XmlIgnore on generated partial classes

查看:236
本文介绍了上产生局部类使用XmlIgnore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ 2 SQL生成的类,我想通过一个web服务暴露。
有一些内部属性我不想用。

I've got a LINQ 2 SQL generated class I'd like to expose through a webservice. There are some internal properties I don't want to be available.

一般情况下我扔[XmlIgnore]在那里,但因为属性在生成的一半,我不能这样做。

Normally I'd throw [XmlIgnore] in there but because the properties are in the generated half I can't do that.

我一直在寻找使用MetadataType以下<一个href=\"http://stackoverflow.com/questions/393687/how-can-i-add-my-attributes-to-$c$c-generated-linq2sql-classes-properties/393690\">this帖子它看起来像它应该允许我在另一个类中定义属性的属性。

I've been looking at using MetadataType following this post which looks like it should allow me to define the property attributes in another class.

我的code看起来是这样的:

My code looks something like this:

[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; }
}

我通过ASP.NET Web服务从Silverlight项目引用这一点。

I'm referencing this through an ASP.NET Web Service from a Silverlight Project.

问题是,[XmlIgnore]属性被忽略,这些属性正在通过发送。

The issue is that the [XmlIgnore] attributes are being ignored, those properties are being sent through.

有没有人有任何深入了解什么可能是错误怎么回事?和什么可能是做到这一点的最好方法是什么?

Does anyone have any insight into what might be going wrong here? and what might be the best way to do this?

推荐答案

据我所知, MetadataTypeAttribute 不受的XmlSerializer (虽然这将是很好 - 我根本不会选中)。正如你说,你不能在局部类添加成员属性。

AFAIK, MetadataTypeAttribute is not supported by XmlSerializer (although it would be nice - I've simply never checked). And as you say, you can't add member attributes in a partial class.

一种选择可能是使生成的属性非公有制(私人保护内部) - 并将其命名为类似 TimestampStorage (ETC) - 然后在公共API重新暴露它们(在部分类)

One option may be to make the generated properties non-public (private, protected or internal) - and name it something like TimestampStorage (etc) - then re-expose them (in the partial class) on the public API:

 [XmlIgnore]
 public object Timestamp {
     get {return TimestampStorage; }
     set {TimestampStorage = value; }
 }
 // and other properties

(因为的XmlSerializer 只着眼于公共API)。这里最大的问题是,LINQ到SQL查询(其中,等)将只对生成列工作( TimestampStorage 等)。我用这个方法之前,成员为内部,让我DAL类使用内部属性.. ,但它是一个有点忽悠的。

(since XmlSerializer only looks at the public API). The biggest problem here is that LINQ-to-SQL queries (Where etc) will only work against the generated columns (TimestampStorage etc). I've used this approach before with the member as internal, allowing my DAL class to use the internal property... but it is a bit of a fudge.

这篇关于上产生局部类使用XmlIgnore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆