2013-10-29 22 views
0

我正在使用SavableModelBase爲了保存/加載配置文件到/從XML。Catel SavableModelBase和繼承

我現在有一個案例,我有我想要重構基類的共同屬性。

是這樣的:

class CommonConfig: SavableModelBase<CommonConfig> 
{ 
    /// <summary> 
    /// Gets or sets the property value. 
    /// </summary> 
    public string CommonPath 
    { 
     get { return GetValue<string>(CommonPathProperty); } 
     set { SetValue(CommonPathProperty, value); } 
    } 

    /// <summary> 
    /// Register the Name property so it is known in the class. 
    /// </summary> 
    public static readonly PropertyData CommonPathProperty = RegisterProperty("CommonPath", typeof(string), string.Empty); 
} 

然後我想創造某些特定配置(例如SpecificConfig)與共同配置該共享屬性。如果我繼承CommonConfigSave()函數的問題不知道SpecificConfig的屬性。

我想我可以使用作文(SpecificConfig將有一個CommonConfig類型的財產),但這不看/讀得很好。

有什麼建議嗎?

回答

0

在使用上面的場景:

SpecificConfig.Load(myFile, SerializationMode.Xml); 

應該返回CommonConfig。可能它返回null,因爲文件中的類實際上是SpecificConfig

相反,如果我使用:

SpecificConfig.Load<SpecificConfig>(myFile, SerializationMode.Xml); 

它按預期工作。

0

序列化引擎使用GetType()來獲取實際類型。所以你可以使用接口/基類/無論你想實際調用Save()方法。最後,它將獲得實際活動實例類型的屬性(從而繼承),並保存屬於該對象的所有屬性,包括基類屬性。

換句話說,這應該很好。

+0

我試了一次,似乎沒有工作。也許問題出在負載上?如果我使用SavanleModelBase 加載的類型應該是XML文件中的任何內容? –

+0

不,它必須有一個類才能在xml文件中查看它是否可以加載之前實例化。請創建一個可重現的問題並上傳到http://www.catelproject.com/support/issue-tracker/,然後我可以看看它。 –

+0

嗨 - 直到現在還沒有回到它 - 我發現如果我使用SpecificConfig.Load (...)它可以工作,但使用SpecificConfig.Load(...)不起作用 - 它這個如預期? –