473,383 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

expandableObjectConvertors TypeConverters

I have an object which I present to the user through a propertyGrid. With
many of the properties (many of which are objects themselves) I have
implemented my own TypeConverters; many of these inherit from the
ExpandableObjectConverter so that child properties are available to the user.

That all works swell.

Now I'm trying to create a print engine that will be able to print all
properties (Browsable). I thought this was going to be straightforward but I
have hit a snag....

I want to be able to go through the properties of my object and access the
all the child objects and their properties (essentially expanding them for my
print output)
Using the TypeDescriptor.GetProperties() I am able to get a collection of
all the 'first level Properties. I thought that getting at the Child object
properties would be as simple as using the TypeDescriptor.GetConverter()
method. This returns the TypeConvertor but I do not know how to determine if
it is of the expandable variety.

I tried looking at how the property grid accomplishes this but am having
difficulty. It looks like it uses the GetPropertiesSupported() Method on the
TypeDescriptor to determine if the object is expandable. I am trying this by
overriding the GetPropertiesSupported(ITypeDescriptorContext context) method.
However I don't really understand where I get the context from. I need an
example.

If anyone can help it would be appreciated.
Jul 12 '07 #1
3 3408
Actually, TypeConverter.GetProperties is the exception for listing
properties; the "normal" approach is

TypeDescriptor.GetProperties(obj) or TypeDescriptor.GetProperties(obj,
attribs)

Note that neither requires a context - just a value - so you can
recurse i.e.

void ShowProps(object parentObj) {
foreach(PropertyDescriptor prop in
TypeDescriptor.GetProperties(obj)) {
object childObj = prop.GetValue(parentObj);
if(childObj != null) {
ShowProps(childObj);
}
// do something meaningful - i.e. WriteLine(prop.Name + "="
Convert.ToString(childObj));
}
}

Note, however, that for getting the string, the TypeConverter often
*is* the better approach (some conversions depend on the context); you
can write your own ITypeDescriptorContext class very easily (as
below), and use as:

Context ctx = new Context(parentObj, prop);

Make sense?

Marc

[ImmutableObject(true)]
public sealed class Context : ITypeDescriptorContext {
private readonly object component;
private readonly PropertyDescriptor property;
public Context(object component, PropertyDescriptor property)
{
this.component = component;
this.property = property;
}
IContainer ITypeDescriptorContext.Container {
get { return null; }
}

object ITypeDescriptorContext.Instance {
get { return component; }
}

void ITypeDescriptorContext.OnComponentChanged() {
}

bool ITypeDescriptorContext.OnComponentChanging() {
return true;
}

PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor {
get { return property; }
}

object IServiceProvider.GetService(Type serviceType) {
return null;
}
}
I have an object which I present to the user through a propertyGrid. With
many of the properties (many of which are objects themselves) I have
implemented my own TypeConverters; many of these inherit from the
ExpandableObjectConverter so that child properties are available to the user.

That all works swell.

Now I'm trying to create a print engine that will be able to print all
properties (Browsable). I thought this was going to be straightforward but I
have hit a snag....

I want to be able to go through the properties of my object and access the
all the child objects and their properties (essentially expanding them for my
print output)
Using the TypeDescriptor.GetProperties() I am able to get a collection of
all the 'first level Properties. I thought that getting at the Child object
properties would be as simple as using the TypeDescriptor.GetConverter()
method. This returns the TypeConvertor but I do not know how to determine if
it is of the expandable variety.

I tried looking at how the property grid accomplishes this but am having
difficulty. It looks like it uses the GetPropertiesSupported() Method on the
TypeDescriptor to determine if the object is expandable. I am trying this by
overriding the GetPropertiesSupported(ITypeDescriptorContext context) method.
However I don't really understand where I get the context from. I need an
example.

If anyone can help it would be appreciated.

Jul 13 '07 #2
Thanks for the quick reply Marc,

Thanks for the info on the context. I thought that I had to get the context
from something existing... It didn't occur I just could create my own.

I definitely need access to the converter for this situation. However I
found that this method works...

I figured the 'appropriate' way to do this was to see if the TypeConverter
inherited the ExpandableObjectConvertor because the propertyGrid requires
this if the property can be expanded.

My problem was that the GetConverter Method returns a type of TypeConverter
by upcasting its converter. So if I tried
if (TypeDescriptor.GetConverter(property) is ExpandableObjectConverter){}
It would never return true.

So then I thought that the mechanism that the property grid used to may be
to call the GetPropertiesSupported() Method. (Presuambly an expandable
object would return true) (!!! I don't know if this is what happens by the
way, it would be super if MSDN described how this works somewhere but I
couldn't find anything). Thats when I was left in a quandry converning the
context argument.

I had a small but relevant breakthrough after I posted my original message.
I saw that, in the watch window, it knew about the converter that I had
decorated my object with. It turns out it is exposed as a property. So all
I had to do was..

foreach (PropertyDescriptor property in pdc)
{
if (property.Converter is ExpandableObjectConverter)
{
...
}

I am left wondering why does the GetConverter Method exist if they expose
the Converter as a property? Was the property exposed in a new version
leaving the method for legacy support?

Thaks again.

Greg


"Marc Gravell" wrote:
Actually, TypeConverter.GetProperties is the exception for listing
properties; the "normal" approach is

TypeDescriptor.GetProperties(obj) or TypeDescriptor.GetProperties(obj,
attribs)

Note that neither requires a context - just a value - so you can
recurse i.e.

void ShowProps(object parentObj) {
foreach(PropertyDescriptor prop in
TypeDescriptor.GetProperties(obj)) {
object childObj = prop.GetValue(parentObj);
if(childObj != null) {
ShowProps(childObj);
}
// do something meaningful - i.e. WriteLine(prop.Name + "="
Convert.ToString(childObj));
}
}

Note, however, that for getting the string, the TypeConverter often
*is* the better approach (some conversions depend on the context); you
can write your own ITypeDescriptorContext class very easily (as
below), and use as:

Context ctx = new Context(parentObj, prop);

Make sense?

Marc

[ImmutableObject(true)]
public sealed class Context : ITypeDescriptorContext {
private readonly object component;
private readonly PropertyDescriptor property;
public Context(object component, PropertyDescriptor property)
{
this.component = component;
this.property = property;
}
IContainer ITypeDescriptorContext.Container {
get { return null; }
}

object ITypeDescriptorContext.Instance {
get { return component; }
}

void ITypeDescriptorContext.OnComponentChanged() {
}

bool ITypeDescriptorContext.OnComponentChanging() {
return true;
}

PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor {
get { return property; }
}

object IServiceProvider.GetService(Type serviceType) {
return null;
}
}
I have an object which I present to the user through a propertyGrid. With
many of the properties (many of which are objects themselves) I have
implemented my own TypeConverters; many of these inherit from the
ExpandableObjectConverter so that child properties are available to the user.

That all works swell.

Now I'm trying to create a print engine that will be able to print all
properties (Browsable). I thought this was going to be straightforward but I
have hit a snag....

I want to be able to go through the properties of my object and access the
all the child objects and their properties (essentially expanding them for my
print output)
Using the TypeDescriptor.GetProperties() I am able to get a collection of
all the 'first level Properties. I thought that getting at the Child object
properties would be as simple as using the TypeDescriptor.GetConverter()
method. This returns the TypeConvertor but I do not know how to determine if
it is of the expandable variety.

I tried looking at how the property grid accomplishes this but am having
difficulty. It looks like it uses the GetPropertiesSupported() Method on the
TypeDescriptor to determine if the object is expandable. I am trying this by
overriding the GetPropertiesSupported(ITypeDescriptorContext context) method.
However I don't really understand where I get the context from. I need an
example.

If anyone can help it would be appreciated.


Jul 13 '07 #3
because the
propertyGrid requires this if the property can be expanded
I was under the impression that simply implementing
GetPropertiesSupported and GetProperties is sufficient. Unless I am
mistaken, ExpandableObjectConverter simply makes this easy (for simple
cases) by exposing the properties as per
TypeDescriptor.GetProperties - but I would *not* assume that only
ExpandableObjectConverter (and subtypes) can be expanded.
GetConverter Method returns a type of
TypeConverter by upcasting its converter.
Upcasting should be irrelevant; "is" should see through this... the
variable type and the object type are largely unrelated, unless the
variable type is sealed etc (and so the compiler knows it can't be
subclassed).
I tried
if (TypeDescriptor.GetConverter(property) is
ExpandableObjectConverter){}
You need "property.Converter"; the code you have posted asks for the
type-converter for the PropertyDescriptor class, which is not what you
want.
I am left wondering why does the GetConverter Method exist if they
expose the Converter as a property?
This is for getting the converter for a free-floating object, perhaps
a variable. This differs from the
converter-in-the-context-of-a-property, since the latter can override:
[TypeConverter(typeof(MyRandomConverter))]
public string SomeProp {...}
if (property.Converter is ExpandableObjectConverter)
I think you need to look at GetPropertiesSupported, not testing
against a type.
it would be super if MSDN described how this works somewhere
Well, they don't mention GetProperties() / GetPropertiesSupported(),
but:
http://msdn2.microsoft.com/en-us/lib...e5(VS.80).aspx
http://msdn2.microsoft.com/en-us/lib...bd(VS.80).aspx
(both linked from
http://msdn2.microsoft.com/en-us/lib...er(vs.80).aspx)

Marc
Jul 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave Girvitz | last post by:
I have a PropertyGrid (Windows Forms App) based component that uses TypeConverters to generate ranges of acceptable values for properties. The idea was that I could download the key/value pairs...
0
by: Eric Eggermann | last post by:
I use a PropertyGrid in my UI. I had a property of type image, and the prop grid popped up an open file dialog where the user could select an image, then display a tiny thumbnail of it, along with...
0
by: Adam Dockter | last post by:
Does anyone know if it is possible to have both a DesignTimeVisible(false) and a TypeConverter attribute together. public class ExplorerGroup : Component {...} I use this object in a...
0
by: S.Sigal | last post by:
Hello: I've been trying to 'organize' the layout of my larger controls by moving variables into instances of subclasses...but it just dawned on me that I might be opening a real can of worms due...
3
by: Rick Voight | last post by:
Hi. I have a class N with two properties. I have a control C with a property of type N. I wrote a Type Converter class for class N that can convert a string into N (splits the string and assign...
0
by: ljlevend | last post by:
I need to create TypeConverters for the floating type drawing structures (i.e., PointF, SizeF and RectangleF) that behave exactly as their integer couterparts (e.g., System.Drawing.PointConverter)....
0
by: herpers | last post by:
Hi, I'm writing a control with a lot of properties, that are not directly supported by the propertygrid. I can work around this by writing typeconverters, that do the job for me, but I think for...
0
by: Shell | last post by:
Hi, So here's my story. I have an enum that's defined so: public enum AccountKind { Savings = 0, Current = 1, }
0
by: Sparky74 | last post by:
I have a number of Int32 properties that I want to display in a PropertyGrid control. The problem I have is that I want to be able to bypass the framework's default validation. If you have an Int32...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.