2016-06-02 15 views
2
[TestFixture] 
public class XmlIgnoreWithNewModifierTest 
{ 
    public class Parent 
    { 
     public int Name { get; set; } 
    } 

    public class Child : Parent 
    { 
     [XmlIgnore] 
     public new int Name 
     { 
      get { throw new NotImplementedException(); } 
     } 
    } 

    [Test] 
    public void Test() 
    { 
     var serializer = new XmlSerializer(typeof(Child)); 
     var stream = new MemoryStream(); 

     // Throws 
     serializer.Serialize(stream, new Child()); 
    } 
} 

Die letzte Zeile Code InvalidOperationException mit einer inneren NotImplementedException werfen würde. Making Parent.Name virtuelle und Child.Name Override hilft nicht.C# - XmlIgnore funktioniert nicht mit überschreiben oder einem neuen Modifikator

Ich frage mich, ob es möglich ist, XmlIgnore funktioniert nur auf Child.Name, aber nicht Parent.Name?

Antwort