Geeks With Blogs


Add to Google

Tim Hibbard CEO for EnGraph software

I have an interface IEntity that all my state objects like Client, Vehicle, Route etc implement.  I am creating a control with a DataGridView that will display information about a list of state objects.  The control expects an IEntity object, but since IEntity only defines methods, nothing is displayed on the DGV when I bind it to List<IEntity>, even though I can see the .DataSource is properly set when I view it in the debugger.

I need to cast my object back to it's original type, so it's properties can be displayed in the DGV.  The code that populates the DGV will not know what the original type is so I can't simply down cast it.  I know there is a simple way to do this, I just can't seem to find it.

The code I have now looks like this:


List<IEntity> results = MyAnonDelegate.Invoke(); dataGridViewResults.DataSource = results;

MyAnonDelegate is obviously an anonymous delegate that is passed to the control to tell it where to get it's data from.

Somehow, I need to get a List<T> where T = the original state object that implements IEntity.  Any pointers?

 Update:  I reached my desired result, but it doesn't seem very elegant.  I used results.ConvertAll and casted each IEntity to an object and my DGV renders it correctly.  Here is the code I used:

List<object> ds = results.ConvertAll(new Converter<IEntity, object>(
 delegate(IEntity ent) { return (object)ent; }
dataGridViewResults.DataSource = ds;


Posted on Friday, May 18, 2007 10:41 AM .NET | Back to top

Comments on this post: Casting an interface to the original type

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Tim Hibbard | Powered by: