2011-09-07 252 views
1

當有基類連載的性質我有幾個屬性基類:序列化派生類

// must include any derived classes here as known types or else they will throw errors on serialization 
[KnownType(typeof(CollaborationEventMeasureDistance))] 
[DataContract] 
public partial class CollaborationEvent 
{ 
    public bool HasBeenTransported { get; set; } 
    public Guid MessageBoxGuid { get; set; } 

    public CollaborationEvent() 
    { 
     HasBeenTransported = false; 
    } 

} 

而且派生類中有自己的一些屬性:

public class CollaborationEventMeasureDistance : CollaborationEvent 
{ 
    public Geometry Geometry { get; set; } 
} 

當我序列化派生類,其所有屬性都被序列化,但它從基類繼承的屬性不是:

<CollaborationEvent i:type="CollaborationEventMeasureDistance"> 
    <Geometry xmlns:d4p1="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry" 
    i:type="d4p1:Polyline"> 
    <d4p1:spatialReference> 
     <d4p1:wkid>26910</d4p1:wkid> 
    </d4p1:spatialReference> 
    <d4p1:paths> 
     <d4p1:points> 
     <d4p1:point> 
      <d4p1:spatialReference> 
      <d4p1:wkid>26910</d4p1:wkid> 
      </d4p1:spatialReference> 
      <d4p1:x>460892.23924271885</d4p1:x> 
      <d4p1:y>5367682.5572773879</d4p1:y> 
     </d4p1:point> 
     <d4p1:point> 
      <d4p1:spatialReference i:nil="true" /> 
      <d4p1:x>461001.35841108358</d4p1:x> 
      <d4p1:y>5367648.5755294543</d4p1:y> 
     </d4p1:point> 
     </d4p1:points> 
    </d4p1:paths> 
    </Geometry> 
</CollaborationEvent> 

任何人都可以指出我做錯了什麼?

我希望我的XML看起來更像:

<CollaborationEvent i:type="CollaborationEventMeasureDistance"> 
    <HasBeenTransported>True</HasBeenTransported> 
    <MessageBoxGuid>blah</MessageBoxGuid> 
    <Geometry xmlns:d4p1="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry" 
    i:type="d4p1:Polyline"> 
    <d4p1:spatialReference> 
     <d4p1:wkid>26910</d4p1:wkid> 
    </d4p1:spatialReference> 
    <d4p1:paths> 
     <d4p1:points> 
     <d4p1:point> 
      <d4p1:spatialReference> 
      <d4p1:wkid>26910</d4p1:wkid> 
      </d4p1:spatialReference> 
      <d4p1:x>460892.23924271885</d4p1:x> 
      <d4p1:y>5367682.5572773879</d4p1:y> 
     </d4p1:point> 
     <d4p1:point> 
      <d4p1:spatialReference i:nil="true" /> 
      <d4p1:x>461001.35841108358</d4p1:x> 
      <d4p1:y>5367648.5755294543</d4p1:y> 
     </d4p1:point> 
     </d4p1:points> 
    </d4p1:paths> 
    </Geometry> 
</CollaborationEvent> 

感謝

+0

他們是定在的時候序列化? –

+0

我們可以看到你的序列化代碼嗎? –

回答

2

假設你的Geometry類是可序列化,嘗試這樣的事情:

[DataContract, Serializable] 
public class CollaborationEventMeasureDistance : CollaborationEvent 
{ 
    [DataMember] 
    public Geometry Geometry { get; set; } 
} 

[KnownType(typeof(CollaborationEventMeasureDistance))] 
[DataContract, Serializable] 
public partial class CollaborationEvent 
{ 
    [DataMember] 
    public bool HasBeenTransported { get; set; } 
    [DataMember] 
    public Guid MessageBoxGuid { get; set; } 

    public CollaborationEvent() 
    { 
     HasBeenTransported = false; 
    } 

} 
+0

謝謝泰勒。我不認爲我需要在屬性上再添加「語法糖」,因爲派生類的屬性在沒有它的情況下序列化。但是[DataMember]屬性足夠清除它。乾杯。 – Jonathan

相關問題