472,989 Members | 2,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Reflection and an overridden class

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 |
BindingFlags.Public |
BindingFlags.Static );
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 12 '05 #1
4 3557
Hi John, I don't think the data grid has such property. The winform or
webform one?

If you want to get property, you could use the GetProperty method
instead.

PropertyInfo pi = dataGrid.GetType().GetProperty("DataGridRows");
Use pi.GetValue(dataGrid, null) instead of Invoke.

Regards,
Thi

Dec 12 '05 #2
>Hi John, I don't think the data grid has such property. The winform or
webform one?


I see it thanks to Reflector; it is an internal property.
The following code allow you to set height of every row to 100:

PropertyInfo pi = dataGrid1.GetType().GetProperty(
"DataGridRows",
BindingFlags.NonPublic | BindingFlags.Instance );
Array rows = (Array) pi.GetValue(dataGrid1, null);
foreach (object row in rows)
{
PropertyInfo info = row.GetType().GetProperty("Height");
info.SetValue(row, 100, null);
}

Hope it helps,
Thi

Dec 12 '05 #3
Hello,

Thanks for your reply, but I'm not sure that you read my post completely. I
am aware that the property I am looking for is internal. For some reason,
reflection is not picking up the property, and I guess it's because I am
using an inherited class. It must still be available somewhere, I would
guess, but I don't know how to get to it. This procedure does not work. pi
is null for this case.

G

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi John, I don't think the data grid has such property. The winform or
webform one?


I see it thanks to Reflector; it is an internal property.
The following code allow you to set height of every row to 100:

PropertyInfo pi = dataGrid1.GetType().GetProperty(
"DataGridRows",
BindingFlags.NonPublic | BindingFlags.Instance );
Array rows = (Array) pi.GetValue(dataGrid1, null);
foreach (object row in rows)
{
PropertyInfo info = row.GetType().GetProperty("Height");
info.SetValue(row, 100, null);
}

Hope it helps,
Thi

Dec 12 '05 #4
Hi John,
I am aware that the property I am looking for is internal Yes, but I am not in my first post. You should have mentioned this info
in your original post :)For some reason,
reflection is not picking up the property It does; have you tried my code above? Actually, I tested it in my
machine (.NET 1.1)and I guess it's because I am
using an inherited class No. Inherited stuff is included by default. If you want, you can
exclude them by specifying BindingFlags.DeclareOnlyThis procedure does not work. pi
is null for this case.

No, pi is not null. I tested it. You need to add some rows to the data
grid first:
DataTable table = new DataTable();
table.Columns.Add("text");
table.Columns.Add("text2");
table.Rows.Add(new object[] {"value1", "value2"});
table.Rows.Add(new object[] {"value3", "value4"});
dataGrid1.DataSource = table;
However, if you does not add, it is an empty array, not null.
I use GetProperty instead, but there is no problem with GetMethod. The
only 2 binding flags you need to specify is BindingFlags.NonPublic |
BindingFlags.Instance, but it still works if you specify more as in
your snippet. I cannot reprocude the behavior (pi/mi is null).

Thi

Dec 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
0
by: mel | last post by:
Hi, If I have an object 'mySuperClass' of type 'CSuperClass', which is derived from 'CBaseClass', and 'mySuperClass' overrides method 'SomeMethod' of 'CBaseClass', is it possible to to use...
1
by: kavboy | last post by:
I have a base class A and two inhereted classes B and C. public class A{ protected virtual bool Method1(){ return true; } public bool IsMethod1Overriden{ get { ???????
4
by: John Richardson | last post by:
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...
11
by: S. I. Becker | last post by:
Is it possible to determine if a function has been overridden by an object, when I have a pointer to that object as it's base class (which is abstract)? The reason I want to do this is that I want...
12
by: Ratko | last post by:
Hi all, I was wondering if something like this is possible. Can a base class somehow know if a certain method has been overridden by the subclass? I appreciate any ideas. Thanks, Ratko
6
by: wink | last post by:
I'd like to determine if a method has been overridden as was asked here: http://www.velocityreviews.com/forums/t564224-determining-whether-a-derived-class-overrides-a-virtual-memberfunction.html...
1
by: nsphelt | last post by:
I am wondering if it is possible to access the underlying property of a base class within a derived that has overridden that property. I am using VB.NET and when I use the MyBase keyword to access...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.