Reflection and an overridden class

J

John Richardson

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.
 
T

Truong Hong Thi

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
 
T

Truong Hong Thi

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
 
J

John Richardson

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
 
T

Truong Hong Thi

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.DeclareOnly
This 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
 

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