Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

repost - Reflection and Inheritance

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Richardson - 14 Dec 2005 10:07 GMT
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.
Truong Hong Thi - 14 Dec 2005 11:34 GMT
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
Marc Gravell - 14 Dec 2005 11:50 GMT
Couldn't you just shorten this to

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

?

Marc

> Hi John,
>
[quoted text clipped - 31 lines]
> Regards,
> Thi
Truong Hong Thi - 14 Dec 2005 12:04 GMT
uh, yes, right.
John Richardson - 14 Dec 2005 20:26 GMT
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.  :)

> uh, yes, right.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.