2 Replies - 366 Views - Last Post: 04 December 2012 - 09:26 AM Rate Topic: -----

#1 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Update element of an array through Reflections [SOLVED]

Posted 03 December 2012 - 04:32 PM

We are currently in the process of building up a test base for spec flow. So all of the values are coming in as strings. I wrote some code to update a property in the Contract object that I will be using. However the Contract object also has a couple of arrays of custom objects. I want to pass in the "type" of the array, the property of an element in that array, the new value, and the index of the object to modify. The issue I'm running into is getting a dynamically typed list, instead of just object.

Contract.cs

public class Contract
{
  public string Name { get; set; }
  public string Status { get; set; }

  public Asset[] Asset { get; set; }
  public Group[] Group { get; set; }

  public Contract( string name, string status ){
    Name = name;
    Status = status
    Asset = new Asset[ 10 ];
    Group = new Group[ 10 ];
  }
}


public class Asset {
        public int ID { get;set;}
        public string Value {get;set;}
}

public class Group{
        public int ID { get;set;}
        public string Value {get;set;}
}



Thats the basic structure of my Contract, here is the client code:

static void Main( string[] args )
{
  Contract c = new Contract( "TestContract", "Default" );
  for( int i = 0; i < 10; i++ )
  {
     c.Asset[ i ] = new Asset( i*10, "base" );
     c.Group[ i ] = new Group( i*100, "newGroup" );
  }
  
  Console.WriteLine( c );
updateContract( c, "Asset", 0, "Value", ".666." );
            updateContract( c, "Group", 0, "Value", ".999." );
            updateContract( c, "Group", 2, "Value", ".999." );
            updateContract( c, "Status", "Awesome" );
Console.WriteLine( c );


}
public static void updateContract( Contract contract, string collection, int index, string property, string value )
        {
            object collectionObject;
            if( collection == "Asset" )
            {
                collectionObject = contract.Assets[ index ];
            }
            else if( collection == "Group" )
            {
                collectionObject = contract.Group[ index ];
            }
            else
            {
                throw new Exception( "Couldn't parse " + collection + " properly" );
            }

            SetPropValue( collectionObject, property, value );

        }


        public static void updateContract( Contract contract, string Property, string value )
        {
            SetPropValue( contract, Property, value );
        }

        private static void SetPropValue( object src, string propName, string value )
        {
            PropertyInfo pi = src.GetType().GetProperty( propName );

            //Gets the type of the property passed in
            Type t = pi.PropertyType;

            //Converts the string to the property type
            object newVal = Convert.ChangeType( value, t );

            //Sets the newly typed object to the property
            pi.SetValue( src, newVal, BindingFlags.SetProperty, null, null, null );
            //pi.SetValue( src, newVal); // In .NET 4.5
        }



Basically I want to remove that if/else block and put something like this in

Quote

object[] objectCollections = (object[]) GetPropValue(contract, collection);
object curCollectionObject = objectCollections[index];
SetPropValue(ref curCollectionObject, property, value);

But thats breaking. Any ideas or help would be greatly appreciated. Sorry for the long post

This post has been edited by SwiftStriker00: 04 December 2012 - 09:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Update element of an array through Reflections [SOLVED]

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Update element of an array through Reflections [SOLVED]

Posted 04 December 2012 - 08:50 AM

Not sure if it's what you are looking for, but the SetValue override that you are using has a parameter for "index". It's the next to last parameter that you are currently passing as null.

http://msdn.microsof...y/axt1ctd9.aspx

The MSDN page has the description of this parameter as...

Quote

Optional index values for indexed properties. This value should be a null reference (Nothing in Visual Basic) for non-indexed properties.

Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Update element of an array through Reflections [SOLVED]

Posted 04 December 2012 - 09:26 AM

SOLVED:

private static void SetPropValue(object src, string collection, int index, 
                                 string property, string value)
{
    PropertyInfo collectionProperty = src.GetType().GetProperty(collection);
    Array array = collectionProperty.GetValue(src, null) as Array;
    object item = array.GetValue(index);
    SetPropValue(item, property, value);
}



Thanks!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1