2013-02-01 66 views
1

我有這個類:我怎麼當前類設置爲返回類型的結果

using System.IO; 
using System.Xml.Serialization; 

    namespace ssscc.Settings 
    { 
     public class AppSettings 
     { 
     private string _companyName; 
     public string CompanyName 
     { 
      set { _companyName = value; } 
      get 
      { 
      if (string.IsNullOrWhiteSpace(_companyName)) 
      { 
       LoadSettings(); 
      } 
      return _companyName; 
      } 
     } 

     private string _companyPhone; 
     public string CompanyPhone 
     { 
      set 
      { 

      _companyPhone = value; 
      } 
      get 
      { 
      if (string.IsNullOrWhiteSpace(_companyPhone)) 
      { 
       LoadSettings(); 
      } 
      return _companyPhone; 
      } 
     } 

     private string GetSettingsFile() 
     { 
      var exePath = System.Windows.Forms.Application.StartupPath; 
      var sharedDirectory = Path.Combine(exePath, "shared"); 
      var settingsDirectory = Path.Combine(sharedDirectory, "settings"); 
      var settingsFile = Path.Combine(settingsDirectory, "ssscc.xml"); 

      if (!Directory.Exists(sharedDirectory)) 
      { 
      Directory.CreateDirectory(sharedDirectory); 
      } 

      if (!Directory.Exists(settingsDirectory)) 
      { 
      Directory.CreateDirectory(settingsDirectory); 
      } 

      return settingsFile; 
     } 

     internal void SaveSettings(AppSettings settings) 
     { 
      var serializer = new XmlSerializer(typeof(AppSettings)); 
      using (var stream = File.OpenWrite(GetSettingsFile())) 
      { 
      serializer.Serialize((Stream) stream, (object) settings); 
      } 
     } 

     internal void LoadSettings() 
     { 
      if (!File.Exists(GetSettingsFile())) 
      { 
      return; 
      } 

      var serializer = new XmlSerializer(typeof(AppSettings)); 
      using (var stream = File.OpenRead(GetSettingsFile())) 
      { 
      var appsetting = (AppSettings) serializer.Deserialize(stream); 
      CompanyPhone = appsetting.CompanyPhone; 
      CompanyName = appsetting.CompanyName; 
      } 
     } 

     } 
    } 

我的問題是關於這個代碼:

var appsetting = (AppSettings) serializer.Deserialize(stream); 
    CompanyPhone = appsetting.CompanyPhone; 
    CompanyName = appsetting.CompanyName; 

我敢肯定有辦法將appsettings直接返回到包含該方法的類,以便我不必循環遍歷每個屬性,例如:

CompanyPhone = appsetting.CompanyPhone; 
    CompanyName = appsetting.CompanyName; 

我可以直接分配屬性而不必維護此代碼嗎?

回答

1

您正在從文件反序列化過程中獲得AppSettings的新實例。你可以使用它,不是嗎?嘗試用這樣的靜態工廠方法來替代LoadSettings

internal static AppSettings GetInstance() 
{ 
    if (!File.Exists(GetSettingsFile())) 
     return null; 

    var serializer = new XmlSerializer(typeof(AppSettings)); 
    using (var stream = File.OpenRead(GetSettingsFile())) 
     return (AppSettings)serializer.Deserialize(stream); 
} 

,同時保存設置,你有沒有需要通過設置對象作爲參數。我想下面的代碼應該做的工作:

internal void SaveSettings() 
{ 
    var serializer = new XmlSerializer(typeof(AppSettings)); 
    using (var stream = File.OpenWrite(GetSettingsFile())) 
     serializer.Serialize((Stream)stream, this); 
} 

使用工廠GetInstance方法來初始化設置(當然,作爲一個例子):

var s = AppSettings.GetInstance(); 
if (s == null) 
{ 
    s = new AppSettings 
    { 
     CompanyName = "MyCompany", 
     CompanyPhone = "######" 
    }; 
    s.SaveSettings(); 
} 

PS:如果屬性的getter和setter沒有其他邏輯(LoadSettings方法已不存在),你可以使用自動屬性:

public string CompanyName { get; set; } 

public string CompanyPhone { get; set; } 

GetSettingsFile可以聲明爲static,因爲它不運行任何的實例類成員:

private static string GetSettingsFile() 
{ 
    //... 
    return settingsFile; 
} 
+0

這就是我正在尋找偉大的答案! – ErocM

+0

順便說一句,如果你使用這個,請注意這個問題是有關這些方法:http://stackoverflow.com/questions/14654527/extra-closing-bracket-in-xml – ErocM

1

你真的需要有延遲加載在這裏,如果沒有,讓你的方法明確:

public class AppSettings 
{ 
    private static readonly XmlSerializer Serializer 
        = new XmlSerializer(typeof(AppSettings)); 

    public string CompanyName { get; set; } 
    public string CompanyPhone { set; get; } 

    private static string GetSettingsFile() 
    { 
     return null; 
    } 

    public static void SaveSettings(AppSettings settings) 
    { 
     using (var stream = File.OpenWrite(GetSettingsFile())) 
      Serializer.Serialize(stream, settings); 
    } 

    internal static AppSettings LoadSettings() 
    { 
     if (!File.Exists(GetSettingsFile())) 
      return null; 

     object appsetting = null; 

     using (var stream = File.OpenRead(GetSettingsFile())) 
      appsetting = Serializer.Deserialize(stream); 

     return appsetting as AppSettings; 
    } 
} 

你可以使用:

var setting = AppSettings.LoadSettings(); 

和:

AppSettings.SaveSettings(setting); 

請注意這裏,創造XmlSerializer每次will get the memory leak

XmlSerializer的構造函數將通過分析使用反射Person類生成一對從XmlSerializationReader派生類和XmlSerializationWriter。它將創建臨時C#文件,將生成的文件編譯爲臨時程序集,然後將該程序集加載到進程中。像這樣的代碼也相對昂貴。因此,XmlSerializer以每種類型爲基礎緩存臨時程序集。這意味着下次創建Person類的XmlSerializer時,會使用緩存的程序集而不是生成的新程序集。

因此,您應該保持XmlSerializer爲靜態。

+0

+1偉大的答案!我剛好碰到他的第一個。 – ErocM

相關問題