覆盖属性与属性 [英] Overriding a property with an attribute

查看:175
本文介绍了覆盖属性与属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来改变属性的序列化行为。

I'm trying to find a way to change the serialization behavior of a property.

可以说我有这样的情况:

Lets say I have a situation like this:

[Serializable]
public class Record
{
   public DateTime LastUpdated {get; set; }

   // other useful properties ...
}

public class EmployeeRecord : Record
{
   public string EmployeeName {get; set; }

   // other useful properties ...
}

现在我要序列EmployeeRecord。我不想从记录类的LastUpdated属性被序列化。 (我想,当我序列化记录,但要LASTUPDATED被序列化)。

Now I want to serialize EmployeeRecord. I don't want the LastUpdated property from the Record class to be serialized. (I do want LastUpdated to be serialized when I serialize Record, though).

首先,我试图通过的的关键字,然后加入XmlIgnore属性隐藏LastUpdated属性:

First I tried hiding the LastUpdated property by using the new keyword and then adding the XmlIgnore attribute:

public class EmployeeRecord : Record
{
   public string EmployeeName {get; set; }

   [XmlIgnore]
   public new DateTime LastUpdated {get; set; }
   // other useful properties ...
}

但没有奏效。然后我试图使基虚拟LASTUPDATED并覆盖它,保持属性:

But that didn't work. Then I tried making the base LastUpdated virtual and overriding it, keeping the attribute:

[Serializable]
public class Record
{
   public virtual DateTime LastUpdated {get; set; }

   // other useful properties ...
}

public class EmployeeRecord : Record
{
   public string EmployeeName {get; set; }

   [XmlIgnore]
   public override DateTime LastUpdated {get; set; }
   // other useful properties ...
}

这也不能工作。在这两个企图忽略LASTUPDATED的XmlIgnore属性,并愉快地走遍序列化的业务。

This didn't work either. In both attempts the LastUpdated ignored the XmlIgnore attribute and happily went about its business of serializing.

有没有办法让我想要做的怎样呢?

Is there a way to make what I'm trying to do happen?

推荐答案

一,[可序列化] ATTR无关与XmlSerializer的。这是一个红色的鲱鱼。 [可序列化]是有意义的System.Runtime.Serialization,而XmlSerializer的生活中的System.Xml.Serialization。如果你装饰你的类[Serializable接口]和你的成员[XmlIgnore]那么你可能混淆自己或code其他读者。

First, the [Serializable] attr has nothing to do with the XmlSerializer. That is a red herring. [Serializable] is meaningful to System.Runtime.Serialization, while the XmlSerializer lives in System.Xml.Serialization. If you are decorating your class with [Serializable] and your members with [XmlIgnore] then you are probably confusing yourself or other readers of your code.

XmlSerialization在.NET是非常灵活的。根据如何序列化正在做,由您直接或间接地,假设由Web服务运行时 - 你有不同的方式来控制的东西。

XmlSerialization in .NET is very flexible. Depending on how the serialization is being done, directly by you or indirectly, let's say by the web services runtime - you have different ways to control things.

一种选择是使用的 propertyName的的指定模式来开启或关闭的XML序列化的属性。假设你有这样的code:

One option is to use the propertyNameSpecified pattern to turn ON or OFF the property in XML Serialization. Suppose you have this code:

public class TypeA
{ 
  public DateTime LastModified;
  [XmlIgnore]
  public bool LastModifiedSpecified;
}

然后,如果LastModifiedSpecified是虚假的一个实例,上次更改时间字段将不会被序列化的实例。在构造函数中为你的类型,你可以设置LastModifiedSpecified总是真的在基型,始终虚假派生类型。实际的布尔 - LastModifiedSpecified - 永远不会被序列化,因为它被标记XmlIgnore

Then, if LastModifiedSpecified is false in an instance, the LastModified field will not be serialized for that instance. In the constructor for your type, you can set LastModifiedSpecified to always be true in the base type, and always false in the derived type. The actual boolean - LastModifiedSpecified - never gets serialized because it is marked XmlIgnore.

这个小动作被记录<一个href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx">here.

您的其他选择是使用XmlAttributeOverrides,这是动态地提供所述一组XML序列化属性的方式(如XmlElementAttribute,XmlIgnoreAttribute,XmlRootAttribute,等等...) - 动态地提供这些属性到串行器在运行时。而不是检查类型本身对于这些属性的XmlSerializer的,会穿过提供给它的构造覆盖的属性列表。

Your other option is to use XmlAttributeOverrides, which is a way of dynamically providing the set of XML serialization attributes (like XmlElementAttribute, XmlIgnoreAttribute, XmlRootAttribute, and so on...) - dynamically providing those attributes to the serializer at runtime. The XmlSerializer, instead of inspecting the type itself for those attributes, will just walk through the list of override attributes provided to its constructor.

    var overrides = new XmlAttributeOverrides();
    // ....fill the overrides here....
    // create a new instance of the serializer specifying overrides
    var s1 = new XmlSerializer(typeof(Foo), overrides);
    // serialize as normal, here.

这示出了更详细地<一href="http://cheeso.members.winisp.net/srcview.aspx?dir=xml-serialization&file=Overrides.cs">here.

在你的情况,你会提供一个XmlIgnoreAttribute作为替代,但只有当序列化派生类型。 (或其他)这工作,只有当你直接实例XmlSerializer的 - 它不会序列化时,由运行时隐式进行,与Web服务的工作。

In your case, you would provide an XmlIgnoreAttribute as an override, but only when serializing the derived type. (or whatever) This works only when you directly instantiate the XmlSerializer - it won't work when serialization is done implicitly by the runtime, as with web services.

干杯!

这篇关于覆盖属性与属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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