dynamic cast?

J

Jamie McQuay

I receive an object from an event and I need to go through a long list
of types to determine the type to cast to.
For example

if (myObject is A)
{
propertyGrid.SelectedObject = (A)myObject;
}
else if (myObject is B)
{
propertyGrid.SelectedObject = (B)myObject;
}


is there a way to do something like the following?

Type myType = myObject.GetType();
propertyGrid.SelectedObject = (myType )myObject;

Thanks,

Jamie
 
N

Nicholas Paldino [.NET/C# MVP]

Jamie,

No, there isn't. However, it doesn't make much sense to me ^why^ you
would do that since the SelectedObject property on the PropertyGrid class is
of type Object, and it's going to make it's own decisions about what to
display based on the type of the object, not what the type of the reference
is that gets assigned to it.
 
J

Jon Skeet [C# MVP]

Jamie McQuay said:
I receive an object from an event and I need to go through a long list
of types to determine the type to cast to.
For example

if (myObject is A)
{
propertyGrid.SelectedObject = (A)myObject;
}
else if (myObject is B)
{
propertyGrid.SelectedObject = (B)myObject;
}


is there a way to do something like the following?

Type myType = myObject.GetType();
propertyGrid.SelectedObject = (myType )myObject;

What difference would you expect it to make? I assume that
propertyGrid.SelectedObject is declared as type "object", so the cast
won't actually make any difference.
 
J

Jamie McQuay

What difference would you expect it to make? I assume that
propertyGrid.SelectedObject is declared as type "object", so the cast
won't actually make any difference.

thanks for the quick replies Jon & Nicholas.

I need to cast because the object is being received from another DLL.
The objects can be different List<differentTypes> lists.

if i simply use:
propertyGrid.SelectedObject = myObject
I see the information of a List (Capacity and Count).

I cast to show the first item in the List. I can not simply write:
propertyGrid.SelectedObject = myObject[0] since this is an object

I need to write
propertyGrid.SelectedObject = ((A)myObject)[0]

I hope that makes it a little clearer.

I am making a custom PropertyGrid View that will not launch a new
editor for each collection, rather it will show the 1st item and write
all the item names in a listview from which the user can change which
item is being viewed.

Thanks,

Jamie
 
M

Marc Gravell

Sounds an odd thing to do, and you can potentially save a lot of time
by learning about custom property-descriptors and faking it... but
then again, maybe not; System.ComponentModel is a thorny beast...

But if the significance if lists, then perhaps simply something like
below:

if (myObject is IListSource) { // special case; obtain
list
myObject = ((IListSource)myObject).GetList();
}
if(myObject is IList) { // lists; show first item
myObject = ((IList)myObject)[0];
}
propertyGrid.SelectedObject = myObject;

Marc
 
N

Nicholas Paldino [.NET/C# MVP]

Jamie,

If the item is indexed in some way, does it implement an interface that
you can cast it to, instead of having to cast to the specific instance?
Something like IList, or ICollection<T>? That way, you can access any
collection that implements one of those interfaces and then use the indexer
on the interface.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jamie McQuay said:
What difference would you expect it to make? I assume that
propertyGrid.SelectedObject is declared as type "object", so the cast
won't actually make any difference.

thanks for the quick replies Jon & Nicholas.

I need to cast because the object is being received from another DLL.
The objects can be different List<differentTypes> lists.

if i simply use:
propertyGrid.SelectedObject = myObject
I see the information of a List (Capacity and Count).

I cast to show the first item in the List. I can not simply write:
propertyGrid.SelectedObject = myObject[0] since this is an object

I need to write
propertyGrid.SelectedObject = ((A)myObject)[0]

I hope that makes it a little clearer.

I am making a custom PropertyGrid View that will not launch a new
editor for each collection, rather it will show the 1st item and write
all the item names in a listview from which the user can change which
item is being viewed.

Thanks,

Jamie
 
J

Jamie McQuay

if (myObject is IListSource) { // special case; obtain
list
myObject = ((IListSource)myObject).GetList();
}
if(myObject is IList) { // lists; show first item
myObject = ((IList)myObject)[0];
}
propertyGrid.SelectedObject = myObject;

Hi Marc,

Perfect, this is what I'm looking for... thanks.

This is different yes... I'm displaying an xml based file format that
cantains lots of steps and lots of subitems. When I click on a
collection in the propertygrid I have a custom editor that loads the
names of each item in the collection into a listBox and the
propertygrid is set the the first one.

Thanks everyone,

Jamie
 
M

Marc Gravell

This is different yes... I'm displaying an xml based file format that
cantains lots of steps and lots of subitems.  When I click on a
collection in the propertygrid I have a custom editor that loads the
names of each item in the collection into a listBox and the
propertygrid is set the the first one.

If you insist... but PropertyGrid has good support for UITypeEditor
and TypeConverter [noting in particular GetStandardValues] which can
be made to do an awful lot of what you have just described... but if
you are happy...

Marc
 
J

Jamie McQuay

If the item is indexed in some way, does it implement an interface that
you can cast it to, instead of having to cast to the specific instance?
Something like IList, or ICollection<T>? That way, you can access any
collection that implements one of those interfaces and then use the indexer
on the interface.

Using the IList interface did the trick... thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top