I agree with the above, Interfaces are the best solution in this case, however if you want to make it hard on yourself then you can use reflection?
Main Topics
Browse All TopicsI am using inference to loop through an item source. This item source could contain serveral different object types. These objects have different attributes, but one of them remains consistent, and that is "desc". desc is a string that represents the description of the object.
My question is, how can I access the attribute "desc" when using inference? So, If I knew the object was of type "dog", I could say dog.desc, and It would return "collie". But, since the object is generic, I can't access this attribute. Please see the code below and you will see what I am talking about.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
OK, but I'm curious as to why you can't use an interface? Here is an example of using it with reflection
foreach (var item in dg.ItemsSource)
{
dg.SelectedItem = item.GetType().GetProperty
}
If the item object doesn't have the desc property then you're going to get an exception.
it looks, but it's late binding, it's slower, and from a "cleaner" standpoint it's not, as a developer I prefer the interface method as it's static. But hey it's up to you, saves development time. If you like this sort of thing then you may want to use VB.NET instead. In vb.net it would look something like this:
For Each item In dg.ItemsSource
dg.SelectedItem = item.desc
Next
VB.NET allows you to late bind out of the box. You will notice that you don't get intellisense for the "desc" property though, but you can still write it out and run.
Thank you for the points. I hope things work-out.
Interfaces was just the first thing I thought of. I really liked Omega2K's reflection solution as well.
Perhaps it is a question of architecture vs -- just a quick and dirty (but very valid) solution to get the problem resolved. Both have advantages. In other words, do you see yourself running into this problem a lot? Is this like a rare, one time thing, where you just need to get the info out, and will never be needing this again?
Business Accounts
Answer for Membership
by: knowltonPosted on 2009-09-30 at 16:38:34ID: 25464731
Can you have the classes use an interface?
Select allOpen in new window