Oops; TypeConverter is *sub*properties, not the first-order
properties. My bad - should use TypeDescriptor after all...
I'll make it up to you ;-p Here's an example including an
ITypeDescriptorContext:
using System;
using System.ComponentModel;
using System.Windows.Forms;
class Program
{
static void Main()
{
Form obj = new Form(); // why not...
// set some props, just to see how they work
obj.Name = "Some Name";
obj.Text = "Some Text";
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(obj))
{
object val = prop.GetValue(obj);
string displayName = prop.DisplayName;
if(string.IsNullOrEmpty(displayName)) displayName =
prop.Name;
SimpleContext ctx = new SimpleContext(obj, prop);
bool dirty = prop.ShouldSerializeValue(obj);
char dirtyMarker = dirty ? '*' : ' ';
string sVal = prop.Converter.ConvertToString(ctx, val);
Console.WriteLine("{0} {1}={2}", dirtyMarker, displayName,
sVal);
}
}
}
class SimpleContext : ITypeDescriptorContext
{
private readonly object instance;
private readonly PropertyDescriptor property;
public SimpleContext(object instance, PropertyDescriptor property)
{
this.instance = instance;
this.property = property;
}
bool ITypeDescriptorContext.OnComponentChanging() { return true; }
void ITypeDescriptorContext.OnComponentChanged() { }
IContainer ITypeDescriptorContext.Container { get { return
null; } }
public object Instance { get { return instance; } }
public PropertyDescriptor PropertyDescriptor { get { return
property; } }
object IServiceProvider.GetService(Type type) { return null; }
}