PC Review


Reply
Thread Tools Rate Thread

repost - Reflection and Inheritance

 
 
John Richardson
Guest
Posts: n/a
 
      14th Dec 2005
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.




 
Reply With Quote
 
 
 
 
Truong Hong Thi
Guest
Posts: n/a
 
      14th Dec 2005
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

 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      14th Dec 2005
Couldn't you just shorten this to

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

?

Marc


"Truong Hong Thi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Truong Hong Thi
Guest
Posts: n/a
 
      14th Dec 2005
uh, yes, right.

 
Reply With Quote
 
John Richardson
Guest
Posts: n/a
 
      14th Dec 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> uh, yes, right.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reflection and Inheritance question. Frank O'Hara Microsoft Dot NET Framework 1 26th Mar 2007 02:30 PM
Reflection, Custom Attributes, Inheritance Advice Needed Lee Microsoft C# .NET 1 13th Oct 2005 09:05 PM
Serialization, Reflection and Inheritance question aegis@bigpond.net.au Microsoft C# .NET 0 31st Aug 2005 07:54 AM
Persistance using attributes, reflection and inheritance Tilted Microsoft C# .NET 2 25th Nov 2004 09:27 PM
inheritance and reflection Michael Dybevick Microsoft Dot NET Framework 4 10th Dec 2003 05:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:09 PM.