我想弄清楚如何通過反射將任意數組或集合轉換爲字符串,並且它將驅使我堅果.NUTS ...我關於yay接近把我的紅色揮杆線穿過電腦顯示器。例如,給定一個Color對象數組,我希望使用ArrayConverter或ColorConverter或任何適當的轉換器的數組的默認字符串表示形式(您知道,用分號分隔或任何其他形式)。我可以爲簡單的對象類型做這件事,但收藏無法迴避。使用反射/類型轉換將C#數組轉換爲字符串
以下是我如何使用反射迭代(任意)對象的屬性。如何使用適當的轉換器將包含任意類型的數組一般轉換爲標準字符串表示形式?
Type t = widget.GetType();
System.Reflection.PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo prop in props)
{
TypeConverter converter = TypeDescriptor.GetConverter(prop.PropertyType);
if (converter != null)
{
object o = prop.GetValue(widget, null);
att.Value = converter.ConvertToString(o);
// This returns some BS like "System.Array [2]"
// I need the actual data.
}
}
編輯:如果我試試這個:
att.Value = o.ToString();
它返回 「的System.Drawing.Color []」。而我想要「255,202,101; 127,127,127」或任何默認的字符串表示形式,例如屬性編輯器。
謝謝!
如果你問的VS調試可視化工具,我不認爲這些是在可重新分配的CLR。 – 2009-07-29 15:58:00