473,386 Members | 1,835 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,386 software developers and data experts.

dynamic cast?


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
Feb 21 '08 #1
4 1717
Re my comment about faking it with PropertyDescriptor, see here:
http://www.codeproject.com/KB/tabs/c...ctiondata.aspx

Marc
Feb 21 '08 #2
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

Feb 21 '08 #3
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
Feb 21 '08 #4
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.
Feb 21 '08 #5

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

Similar topics

5
by: adrian | last post by:
hi all this is my first post to this group, so pls bear with me while i try to make some sense. i am trying to create a sproc which uses dynamic sql to target a particuar table eg. '.' and...
4
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure...
2
by: Bane | last post by:
Hi all In the SP below im (trying to) do some dynamic sql. As you can see the table to use is set as a variable and the 'exec' method used to run the sqlstatements. My problem is that the 'if...
0
by: raca | last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an XML string that will be used to deserialize an object generated by XSDObjectGen. The hierarchy goes like this:...
9
by: Gibby Koldenhof | last post by:
Hiya, Terrible subject but I haven't got a better term at the moment. I've been building up my own library of functionality (all nice conforming ISO C) for over 6 years and decided to adopt a...
2
by: Martin Hart - Memory Soft, S.L. | last post by:
Hi all: I still very new to the .NET world and don't know if what I am asking is due to an over-imaginative imagination or the fact that I have read too many fiction books!! Let me show you a...
6
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
6
by: DaTurk | last post by:
Hi, I have three interfaces lets call them parent, child-A, and child-B. I want to have one variable, that can be either child-A, or child-B at run time. I just can't seem to figure out how to...
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
1
by: Pacific Fox | last post by:
Hi all, I have a SQL statement that allows paging and dynamic sorting of the columns, but what I can't figure out without making the SQL a dynamic string and executing it, or duplicating the SQL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.