2011-12-02 98 views
3

我想知道,有什麼方法可以覆蓋ToString()類字段的方法,類型爲byte[]?我甚至不知道這是可能的......下面是示例代碼:覆蓋類字段的ToString()方法

public class Object 
{ 
    private byte[] _content; 

    public byte[] Content 
    { 
     get 
     { 
      return _content; 
     } 

     internal set 
     { 
      _content = value; 
     } 
    } 
} 

是否有可能重寫的方式Object.Content.ToString()方法怎麼叫?

編輯:爲什麼我需要它?我想Content可能是純二進制數據,但它也可以是純字符串。如果我需要文字表示,我會使用Encoding.UT8.GetString(Object.Content),但嘿!那麼ToString()它完全適合我的目標,也許我可以以某種方式使用它?

明顯的方法是彈出我的頭 - 圍繞byte[]創建一個包裝並覆蓋它的ToString(),但這對我來說看起來很醜,我認爲它不值得。

有沒有什麼優雅的方法來解決這個問題?或者解決它?

非常感謝提前!

+5

沒有其他辦法。但是,真的,你爲什麼想這樣做? –

+0

也許有可能通過擴展方法?例如。 '公共靜態ToString(這個字節[]){...}' - 但如果它的工作,我不知道它將如何框值... – mfeineis

+0

如果你想對象本身由內容,你可以改寫Object.ToString()。除非你想同時使用,或者如果你在對象中有多種內容。 –

回答

5

不,你不能重寫或者一般更換的ToString()的字節數組實現或Content特性。爲了得到你想要的結果,你可能需要包裝你已經聲明你不想做的數組。

你也可以提供另一個屬性的類內有效地遮蔽Content屬性,如

public string ContentAsString 
{ 
    get 
    { 
     return /* custom string output of this.Content here */ 
    } 
} 

另外一個選擇是延伸字節數組(通過擴展方法),以提供一個字符串轉換的自定義實現(以不同的名稱)(與上述意思相同,表達方式不同)

static class MyExtensions 
{ 
    public static string ToCustomString(this byte[] array) 
    { 
     return ... 
    } 
} 

string result = obj.Content.ToCustomString(); 
+0

我喜歡使用擴展的想法。將嘗試它。謝謝! – shytikov

2

這取決於上下文!如果這裏的情境是「dipslay東西在PropertyGrid中或DataGridView的有意義的」,那麼你也許可以用一個類型轉換器:

[STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.Run(new Form {Controls = { 
      new PropertyGrid {SelectedObject = new SomeType 
      { 
       Content = new byte[] {0,1,2,3,4,5}            
      }, Dock = DockStyle.Fill} 
     }}); 
    } 

    public class SomeType 
    { 
     private byte[] _content; 
     [TypeConverter(typeof(HexConverter))] 
     public byte[] Content 
     { 
      get 
      { 
       return _content; 
      } 

      internal set 
      { 
       _content = value; 
      } 
     } 
    } 
    class HexConverter : TypeConverter 
    { 
     public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) 
     { 
      if(destinationType == typeof(string)) 
      { 
       if (value == null) return ""; 
       byte[] raw = (byte[]) value; 
       var sb = new StringBuilder(2*raw.Length); 
       for (int i = 0; i < raw.Length; i++) sb.Append(raw[i].ToString("x2")); 
       return sb.ToString(); 
      } 
      return base.ConvertTo(context, culture, value, destinationType); 
     } 
    } 
1

As @D anielHigarth指出,有沒有真正的好辦法做到這一點,我不知道你爲什麼會想......

但是,你可以:

使用工具如postsharp攔截所有致電byte[].ToString()從您的程序集並返回任何你喜歡的,而不是實際的結果。

使用擴展方法如

public static string ToElegantString(this byte[] bytes) { 
{ 
    return "ElegantString"; 
} 
+0

這種方式有什麼缺點?我在問,因爲這很接近我想要的。 – shytikov

0

怎樣的約,而不是重寫的ToString()方法,只需修改get函數?

例如:

public string Content 
{ 
    get 
    { 
     return YourByteFormatter(_content); 
    }  
} 
+0

我需要在'byte []'和'string'表示之間切換。 – shytikov

0

定義你的字節數組作爲一個單獨的類別,所以你可以給它一個ToString()重寫。