repost - Reflection and Inheritance

J

John Richardson

My original posting (Dec 12, this group) is copied below, but it boils down
to a simple question first:
does an inherited class prevent certain internal properties (inherited from
an ancestor class) from being visible to GetType().GetMethod()?

And can I call GetMethod from the context of the ancestor?

Basically, can I get the following to work?

----------------------------------------------------------------------------------

I haven't played much with Reflection before, so I'm hoping I'm missing
something simple with this problem I'm having.

Trying to follow the online examples for resizing a DataGrid row height, and
the following is the snippet:

------
MethodInfo mi = dataGrid.GetType().GetMethod( "get_DataGridRows",
BindingFlags.FlattenHierarchy |
BindingFlags.IgnoreCase |
BindingFlags.Instance |
BindingFlags.NonPublic);
Array dataGridArray = (Array) mi.Invoke( dataGrid, null );
------

But, my dataGrid in this case is an overridden System.Windows.Forms.DataGrid
class, and GetMethod does not find my "get_DataGridRows" for my inherrited
class. mi is null for the above execution. I've tried seeing if I can cast
the type to the lower one, but I must be too tired to figure it out, so
hopefully someone here can assist me.

Thanks.
 
T

Truong Hong Thi

Hi John,

Because I already answered your prev post but it did not help, I try it
again.
It was my mistake that did not take your sentence "But, my dataGrid in
this case is an overridden System.Windows.Forms.DataGrid class" into
account.

Because it is "internal", not "protected", property, and your assembly
is different from System.Windows.Form.dll, the answer is no.

You can try this snippet instead:
MethodInfo mi = dataGrid.GetType().BaseType.GetMethod(
"get_DataGridRows",
BindingFlags.Instance | BindingFlags.NonPublic);
Array dataGridArray = (Array) mi.Invoke( dataGrid, null );

Note I add "BaseType" after GetType() because your data grid is
directly derived from System.Windows.Forms.DataGrid. If not, you can
call continuously calling BaseType until you get to
System.Windows.Forms.DataGrid, like this:

Type t = dataGrid.GetType().BaseType;
while (t != typeof(DataGrid))
{
t = t.BaseType;
}
MethodInfo mi = t.GetMethod("get_DataGridRows",
BindingFlags.Instance | BindingFlags.NonPublic);
Array dataGridArray = (Array) mi.Invoke( dataGrid, null );

Hope this time I can help,
Let me know if it still does not :)
Regards,
Thi
 
M

Marc Gravell

Couldn't you just shorten this to

typeof(Windows.Forms.DataGrid).GetMethod(...) etc

?

Marc
 
J

John Richardson

ok... yeah, I figured it was an easy one, just didn't see it. thanks a lot.

I had tried to cast it: DataGrid g = myGrid as DataGrid and then going
through it, but I guess no matter the casted type, getType() seems to return
the most "recent" type, for lack of a better term, which is what I would
expect anyways.

As an aside, Thi, sorry about the repost. I can't believe how busy this
group is getting. It hasn't been that long and already my (original) post
is way down the list. I figured you wouldn't get the chance to see my
reply. :)
 

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