473,624 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

expandableObjec tConvertors 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
ExpandableObjec tConverter 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 GetPropertiesSu pported() Method on the
TypeDescriptor to determine if the object is expandable. I am trying this by
overriding the GetPropertiesSu pported(ITypeDe scriptorContext 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 3453
Actually, TypeConverter.G etProperties is the exception for listing
properties; the "normal" approach is

TypeDescriptor. GetProperties(o bj) or TypeDescriptor. GetProperties(o bj,
attribs)

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

void ShowProps(objec t parentObj) {
foreach(Propert yDescriptor prop in
TypeDescriptor. GetProperties(o bj)) {
object childObj = prop.GetValue(p arentObj);
if(childObj != null) {
ShowProps(child Obj);
}
// do something meaningful - i.e. WriteLine(prop. Name + "="
Convert.ToStrin g(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 ITypeDescriptor Context class very easily (as
below), and use as:

Context ctx = new Context(parentO bj, prop);

Make sense?

Marc

[ImmutableObject (true)]
public sealed class Context : ITypeDescriptor Context {
private readonly object component;
private readonly PropertyDescrip tor property;
public Context(object component, PropertyDescrip tor property)
{
this.component = component;
this.property = property;
}
IContainer ITypeDescriptor Context.Contain er {
get { return null; }
}

object ITypeDescriptor Context.Instanc e {
get { return component; }
}

void ITypeDescriptor Context.OnCompo nentChanged() {
}

bool ITypeDescriptor Context.OnCompo nentChanging() {
return true;
}

PropertyDescrip tor ITypeDescriptor Context.Propert yDescriptor {
get { return property; }
}

object IServiceProvide r.GetService(Ty pe 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
ExpandableObjec tConverter 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 GetPropertiesSu pported() Method on the
TypeDescriptor to determine if the object is expandable. I am trying this by
overriding the GetPropertiesSu pported(ITypeDe scriptorContext 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 ExpandableObjec tConvertor 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(p roperty) is ExpandableObjec tConverter){}
It would never return true.

So then I thought that the mechanism that the property grid used to may be
to call the GetPropertiesSu pported() 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 (PropertyDescri ptor property in pdc)
{
if (property.Conve rter is ExpandableObjec tConverter)
{
...
}

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.G etProperties is the exception for listing
properties; the "normal" approach is

TypeDescriptor. GetProperties(o bj) or TypeDescriptor. GetProperties(o bj,
attribs)

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

void ShowProps(objec t parentObj) {
foreach(Propert yDescriptor prop in
TypeDescriptor. GetProperties(o bj)) {
object childObj = prop.GetValue(p arentObj);
if(childObj != null) {
ShowProps(child Obj);
}
// do something meaningful - i.e. WriteLine(prop. Name + "="
Convert.ToStrin g(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 ITypeDescriptor Context class very easily (as
below), and use as:

Context ctx = new Context(parentO bj, prop);

Make sense?

Marc

[ImmutableObject (true)]
public sealed class Context : ITypeDescriptor Context {
private readonly object component;
private readonly PropertyDescrip tor property;
public Context(object component, PropertyDescrip tor property)
{
this.component = component;
this.property = property;
}
IContainer ITypeDescriptor Context.Contain er {
get { return null; }
}

object ITypeDescriptor Context.Instanc e {
get { return component; }
}

void ITypeDescriptor Context.OnCompo nentChanged() {
}

bool ITypeDescriptor Context.OnCompo nentChanging() {
return true;
}

PropertyDescrip tor ITypeDescriptor Context.Propert yDescriptor {
get { return property; }
}

object IServiceProvide r.GetService(Ty pe 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
ExpandableObjec tConverter 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 GetPropertiesSu pported() Method on the
TypeDescriptor to determine if the object is expandable. I am trying this by
overriding the GetPropertiesSu pported(ITypeDe scriptorContext 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
GetPropertiesSu pported and GetProperties is sufficient. Unless I am
mistaken, ExpandableObjec tConverter simply makes this easy (for simple
cases) by exposing the properties as per
TypeDescriptor. GetProperties - but I would *not* assume that only
ExpandableObjec tConverter (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(p roperty) is
ExpandableObjec tConverter){}
You need "property.Conve rter"; the code you have posted asks for the
type-converter for the PropertyDescrip tor 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(t ypeof(MyRandomC onverter))]
public string SomeProp {...}
if (property.Conve rter is ExpandableObjec tConverter)
I think you need to look at GetPropertiesSu pported, not testing
against a type.
it would be super if MSDN described how this works somewhere
Well, they don't mention GetProperties() / GetPropertiesSu pported(),
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
6041
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 from a database and manipulate the TypeConverters dynamically. The property grid then displays the property with a drop down list. It does work when I open the application for the first time. If, however, I open the form and then open a second...
0
936
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 the image type. I've had to change the type of this property to a new class of my own, which will still contain an image, but I need to handle opening the image myself. So now, in the prop grid, I just get a grayed out string with my classes...
0
2402
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 collection and don't want the objects to be manipulated except by the collection editor. This is why I use the DesignTimeVisible(false). I also want my object to use a constructor with a
0
1450
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 to StateBag serialization... If ViewState serialization is optimized for Int32, Bool, String, Color, and arrays thereof... what happens when I want to serialize the instance of a class that contains an Int32, Bool String, or Color?
3
1462
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 them to the properties). I manage to put a control of type C on a form and update N's value through the designer. But I can't make the following sample code to work:
0
306
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). I am able to create TypeConverters with basic functionality by inheriting from System.ComponentModel.ExpandableObjectConverter and overriding CanConvertTo, CanConvertFrom, ConvertTo and ConvertFrom. But, the integer converts also implement the...
0
862
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 most of the types there should be source code available, that anybody could copy. Does anybody know where to find typeconverters for ColorBlend, StingFormat so on?
0
1278
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
1206
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 property, delete the text within the propertygrid cell, then the default validation error window appears. I want to modify the value to zero if no value is entered. I notice that the ConvertTo/ConvertFrom methods return objects.. which Int32...
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.