Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Update element of an array through Reflections

$
0
0
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

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>