I have a property with a member of type object. I am showing the value of
this property in a PropertyGrid but depending on the type of the object I
want to use the correspondant TypeConverter. Here is an example of what I
mean:

public byte[] Test
{
get { return new byte[100]; }
}

public object Test2
{
get { return new byte[100]; }
}

If I have both properties in a property grid, Test will be shown as an
expandable item with the text "Byte[] Array".. if I expand the item I will
be able to see all of the elements in the array. However, Test2 will only
show the text "System.Byte[]" and I will have no way of reading the
elements of the array.

Of course the property of type object will not always contain a byte[].

How can I provide the standard functionality for each type?

Thanks

Re: TypeConverter for object type Properties by VisualHint

VisualHint
Wed Mar 12 09:00:16 CDT 2008

Hi,

You could set a special TypeConverter which would delegate the work to
the real TypeConverter of the property. When the value is passed in
argument, this is easy. But when only a context is passed, you will
only have the context.Instance property to get the information,
meaning that you will have to cast the instance to its real type to
get access to your property. Here is an example. Note that you could
cache the returned converter instead of requesting it every time:

public class ObjectConverter : TypeConverter
{
public override bool GetPropertiesSupported(ITypeDescriptorContext
context)
{
TypeConverter t =
TypeDescriptor.GetConverter(((MyContainer)context.Instance).MyProperty);
if (t != null)
return t.GetPropertiesSupported(context);

return base.GetPropertiesSupported(context);
}

public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value,
Attribute[] attributes)
{
TypeConverter t = TypeDescriptor.GetConverter(value);
if (t != null)
return t.GetProperties(context, value, attributes);

return base.GetProperties(context, value, attributes);
}
}

Hope this helps. And anyway this is some stuff that you can experiment
with.

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart PropertyGrid for .Net and MFC (http://www.visualhint.com/
index.php/propertygrid)
Microsoft PropertyGrid Resource List - http://www.propertygridresourcelist.com
Home of Smart FieldPackEditor.Net / DateTimePicker replacement (http://
www.visualhint.com/index.php/fieldpackeditor)


On Mar 7, 2:36 am, Jeronimo Bertran
<jeronimo.bert...@newsgroup.nospam> wrote:
> I have a property with a member of type object. I am showing the value of
> this property in aPropertyGridbut depending on the type of the object I
> want to use the correspondant TypeConverter. Here is an example of what I
> mean:
>
> public byte[] Test
> {
> get { return new byte[100]; }
> }
>
> public object Test2
> {
> get { return new byte[100]; }
> }
>
> If I have both properties in a property grid, Test will be shown as an
> expandable item with the text "Byte[] Array".. if I expand the item I will
> be able to see all of the elements in the array. However, Test2 will only
> show the text "System.Byte[]" and I will have no way of reading the
> elements of the array.
>
> Of course the property of type object will not always contain a byte[].
>
> How can I provide the standard functionality for each type?
>
> Thanks


Re: TypeConverter for object type Properties by VisualHint

VisualHint
Wed Mar 12 09:04:59 CDT 2008

oops, no need to be aware of the container class. Here is how to get
the value:

object value = context.PropertyDescriptor.GetValue(context.Instance);
TypeConverter t = TypeDescriptor.GetConverter(value);

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart PropertyGrid for .Net and MFC (http://www.visualhint.com/
index.php/propertygrid)
Microsoft PropertyGrid Resource List - http://www.propertygridresourcelist.com
Home of Smart FieldPackEditor.Net / DateTimePicker replacement (http://
www.visualhint.com/index.php/fieldpackeditor)


On Mar 12, 10:00 am, VisualHint <cadil...@gmail.com> wrote:
> Hi,
>
> You could set a special TypeConverter which would delegate the work to
> the real TypeConverter of the property. When the value is passed in
> argument, this is easy. But when only a context is passed, you will
> only have the context.Instance property to get the information,
> meaning that you will have to cast the instance to its real type to
> get access to your property. Here is an example. Note that you could
> cache the returned converter instead of requesting it every time:
>
> public class ObjectConverter : TypeConverter
> {
> public override bool GetPropertiesSupported(ITypeDescriptorContext
> context)
> {
> TypeConverter t =
> TypeDescriptor.GetConverter(((MyContainer)context.Instance).MyProperty);
> if (t != null)
> return t.GetPropertiesSupported(context);
>
> return base.GetPropertiesSupported(context);
> }
>
> public override PropertyDescriptorCollection
> GetProperties(ITypeDescriptorContext context, object value,
> Attribute[] attributes)
> {
> TypeConverter t = TypeDescriptor.GetConverter(value);
> if (t != null)
> return t.GetProperties(context, value, attributes);
>
> return base.GetProperties(context, value, attributes);
> }
>
> }
>
> Hope this helps. And anyway this is some stuff that you can experiment
> with.
>
> Best regards,
>
> Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
> Home of Smart PropertyGrid for .Net and MFC (http://www.visualhint.com/
> index.php/propertygrid)
> Microsoft PropertyGrid Resource List -http://www.propertygridresourcelist.com
> Home of Smart FieldPackEditor.Net / DateTimePicker replacement (http://www.visualhint.com/index.php/fieldpackeditor)
>
> On Mar 7, 2:36 am, Jeronimo Bertran
>
> <jeronimo.bert...@newsgroup.nospam> wrote:
> > I have a property with a member of type object. I am showing the value of
> > this property in aPropertyGridbut depending on the type of the object I
> > want to use the correspondant TypeConverter. Here is an example of what I
> > mean:
>
> > public byte[] Test
> > {
> > get { return new byte[100]; }
> > }
>
> > public object Test2
> > {
> > get { return new byte[100]; }
> > }
>
> > If I have both properties in a property grid, Test will be shown as an
> > expandable item with the text "Byte[] Array".. if I expand the item I will
> > be able to see all of the elements in the array. However, Test2 will only
> > show the text "System.Byte[]" and I will have no way of reading the
> > elements of the array.
>
> > Of course the property of type object will not always contain a byte[].
>
> > How can I provide the standard functionality for each type?
>
> > Thanks


Re: TypeConverter for object type Properties by Jeronimo

Jeronimo
Fri Mar 14 01:39:34 CDT 2008

Thanks Nicolas