通过反射更新阵列的元素

时间:2012-12-04 14:44:27

标签: c# .net reflection

我们目前正在建立规范流程的测试基础。所以所有的值都以字符串形式出现。我写了一些代码来更新我将使用的Contract对象中的属性。但是,Contract对象还有一些自定义对象数组。我想传递数组的“类型”,该数组中元素的属性,新值以及要修改的对象的索引。我遇到的问题是获取动态类型列表,而不仅仅是对象。

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


这是我的合同的基本结构,这是客户端代码:

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
}

基本上我想删除if / else块并在

中添加这样的东西
object[] objectCollections = (object[]) GetPropValue(contract, collection);
object curCollectionObject = objectCollections[index];
SetPropValue(ref curCollectionObject, property, value);

但那就是打破。任何想法或帮助将不胜感激。对不起,长篇文章

1 个答案:

答案 0 :(得分:2)

无论如何,如果你真的需要通过反思来做到这一点,那么:

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

错误处理取决于您。用法:

SetPropValue(c, "Asset", 2, "Value", ".777.");