日期:2014-05-18  浏览次数:20809 次

求助:怎样对数组类型的类属性进行赋值操作?
这个是动态编译生成的类:
public partial class A
{
  private string[] aGDuringListField;
   
  /// <remarks/>
  [System.Xml.Serialization.XmlArrayAttribute(Order=0)]
  [System.Xml.Serialization.XmlArrayItemAttribute("AG_during", IsNullable=false)]
  public string[] AGDuringList
  {
  get
  {
  return this.aGDuringListField;
  }
  set
  {
  this.aGDuringListField = value;
  }
  }
}

现在我需要实例化类A,并且对A的属性AGDuringList进行赋值,我的数组实例化的代码是这样写的:
public object[] CreateInstances(MyInstaceCreateMode mode)
  {
  try
  {
  if (null == _elementtype)
  {
  return null;
  }

  Random rand = new Random(DateTime.Now.Millisecond);
  int instnum = rand.Next(_minoccurs, _maxoccurs + 1);
  if (0 == instnum)
  {
  return null;
  }
  else
  {
  List<object> objs = new List<object>();
  for (int i = 0; i < instnum; i++)
  {
  objs.Add(CreateInstance(mode, _elementtype));
  }
  return objs.ToArray();
  }
  }
  catch
  {
  return null;
  }
  }

外层调用的接口是这样写的:
PropertyInfo property = kvp.Value._PropertyInfo;
  if (kvp.Value.IsArray)
  {
  property.GetSetMethod().Invoke(obj, new object[] { kvp.Value.CreateInstances(mode) });
  }
  else
  {
  property.SetValue(obj, kvp.Value.CreateInstance(mode), null);
  }

但是调试的时候会提示:无法将System.object[]转为System.String[]

因为我无法确定当前运行的PropertyInfo的类型是什么,所以无法强转,请大虾帮我看看,这个问题应该如何解决?或者我的思路不正确?

------解决方案--------------------
先说为什么要用public string[] AGDuringList
 
------解决方案--------------------
那就进行 一下处理吧
C# code

this.aGDuringListField = value.Cast<string>().ToArray();;