471,310 Members | 1,135 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

repost - Reflection and Inheritance

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.


Dec 14 '05 #1
4 2214
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

Dec 14 '05 #2
Couldn't you just shorten this to

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

?

Marc
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
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

Dec 14 '05 #3
uh, yes, right.

Dec 14 '05 #4
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. :)
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
uh, yes, right.

Dec 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by P Vinodh Kumar | last post: by
4 posts views Thread by Bob | last post: by
8 posts views Thread by Rlrcstr | last post: by
7 posts views Thread by guy | last post: by
4 posts views Thread by Egbert Nierop \(MVP for IIS\) | last post: by
11 posts views Thread by David Veeneman | last post: by
reply views Thread by rosydwin | last post: by

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.