PropertyInfo.GetValue - Problems with Count Property ofType ArrayList

  • Thread starter Thread starter gt
  • Start date Start date
G

gt

I am trying to replicate the functionality of
DataTextField/DataValueFields in an extended DropDownList control that
I have created in order to provide a third value for each ListItem.
This value will be stored in a client-side array currently as that is
where it is required. Now to the problem.

I've overriden the DataBind Method of my DropDownList and within it
have added the code below:

Type DataSourceType = this.DataSource.GetType();
PropertyInfo LengthProperty = DataSourceType.GetProperty( "Count" );
MethodInfo get_ItemMethod = DataSourceType.GetMethod( "get_Item" );

if( LengthProperty != null && get_ItemMethod != null )
{

for( int i = 0; i < Convert.ToInt32( LengthProperty.GetValue(
DataSource, null) ); i++ )
{
object[] Parameters = new object[
get_ItemMethod.GetParameters().Length ];
Parameters[0] = i;
object CurrentObject = get_ItemMethod.Invoke( DataSource,
Parameters );
}
}

Now When I execute this code in debug mode, and break the Statement
LengthProperty.GetValue( DataSource, null) gives me an error in the
Watcher of
'error: arguments do not match parameters for function
'LengthProperty.GetValue''

Can anyone see where I may be going wrong here?
 
gt said:
I am trying to replicate the functionality of
DataTextField/DataValueFields in an extended DropDownList control that
I have created in order to provide a third value for each ListItem.
This value will be stored in a client-side array currently as that is
where it is required. Now to the problem.

<snip>

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.


By the way:
for( int i = 0; i < Convert.ToInt32( LengthProperty.GetValue(
DataSource, null) ); i++ )

That's pretty nastily inefficient - you'll be retrieving the property
by reflection on every iteration of the loop.
 
Back
Top