2009-10-08 42 views
0

我如何在C#中實現類似的功能?Getters和Setters的簡單C#註冊表對象

object Registry; 
Registry = MyProj.Registry.Instance; 

int Value; 
Value = 15; 
Registry.Value = Value; /* Sets it to 15 */ 
Value = 25; 
Value = Registry.Value; /* Returns the 15 */ 

到目前爲止,我有這個對象:

namespace MyProj 
{ 
    internal sealed class Registry 
    { 
     static readonly Registry instance = new Registry(); 

     static Registry() 
     { 
     } 

     Registry() 
     { 
     } 

     public static Registry Instance 
     { 
      get 
      { 
       return instance; 
      } 
     } 
    } 
} 
+1

是關於Windows註冊表中的問題或者說是「Regsitry的名字只是一個conincidence? – M4N 2009-10-08 11:35:20

回答

4

一個簡單的屬性添加到您的註冊類:

internal sealed class Registry 
{ 
    public int Value { get; set; } 
    ... 
} 

然後,使用這樣的:

Registry theRegistry = MyProj.Registry.Instance; 
//note: do not use object as in your question 

int value = 15; 
theRegistry.Value = value; /* Sets it to 15 */ 
value = 25; 
value = theRegistry.Value; /* Returns the 15 */ 
+0

...並使其'靜態';-) – 2009-10-08 11:38:47

+1

@Thomas:使什麼靜態? 註冊表是一個單身人士。 Value屬性是通過靜態Registry.Instance屬性訪問的實例屬性。 – M4N 2009-10-08 11:41:45