471,325 Members | 1,203 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 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 3506
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
4 posts views Thread by John Richardson | last post: by
11 posts views Thread by S. I. Becker | 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.