tags:

views:

78

answers:

1

I'm having trouble grasping reflection in C#, so I'm going to put my specific situation down and see what you guys can come up with. I've read TONS of C# reflection questions on here, but I still just simply don't get it.

So here's my situation; I'm trying to access an array which is a non-public member of a class I have access to.

alt text

Basically it's a System.Collections.CollectionBase which has an array variable called "list", but it has this parent type of OrderCollection and the reflection of it is just confusing the hell out of me.

I have to do a lot of these so a good guide or example would really help. Please let me know if you would like more information.

I blacked out the name of the namespace not because what I'm doing is not illegal by any means, but I'm trying to be first to market on this and so I'm trying to be careful.

+7  A: 

What are you trying to use reflection at all? CollectionBase supports indexing, but only through the explicit interface implementation of IList, so you ought to be able to write:

IList list = Acct.Orders;
response = list[0];

You may need to cast the result to a more appropriate type, but I don't see any need for reflection here.

EDIT: Original answer didn't take account of explicit interface implementation.

Jon Skeet
Jon, I've seen you answer just about every reflection question I've looked up here on StackOverflow. And here you come to the rescue. To answer your question, I don't know why I thought I needed that, guess I just figured it should be harder. I'm kind of new to C# so I'm lost out here so-to-speak. Nevertheless, you're absolutely correct no need for reflection.
Zach
@Zach: No problem. "You don't need to use reflection" is about the most pleasant answer I can ever give to a reflection question - it's nice to have it available, but nicer when you don't need it...
Jon Skeet