Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8928611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:28:56+00:00 2026-06-15T08:28:56+00:00

We are currently in the process of building up a test base for spec

  • 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 ];
  }
}

Asset.cs

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

Group.cs

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:28:57+00:00Added an answer on June 15, 2026 at 8:28 am

    Anyway, if you really need to do this via reflection, then:

    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);
    }
    

    Error handling is up to you. Usage:

    SetPropValue(c, "Asset", 2, "Value", ".777.");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently in the process of building a caching system for some of our
I am building an application and currently am in the process of add backgrounds
We've got a process currently which causes ASP.NET websites to be redeployed. The code
I am currently in the process of updating many test sites on an old
I am currently in the process of porting game code from PC to flash,
I am currently in the process of building a native Google Reader iPhone application
I'm currently in the process of building our ASP.NET C# 3.5 Web site and
I'm currently in the process of building a CRUD tool for an existing Spring-based
I'm currently in the process of building a small shell within C++. A user
I'm currently working on building an automated functional/acceptance test suite for a project, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.