0

我们目前正在建立规范流程的测试基地。所以所有的值都以字符串的形式出现。我编写了一些代码来更新我将使用的 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 ];
  }
}


资产.cs

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


组.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);

但那是打破。任何想法或帮助将不胜感激。对不起,很长的帖子

4

1 回答 1

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.");
于 2012-12-04T15:19:02.510 回答