有一个类,它有35个属性,如下:public class ColumnModel
{
public string Column0 { get; set; }
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }
public string Column7 { get; set; }
public string Column8 { get; set; }
public string Column9 { get; set; }
public string Column10 { get; set; }
public string Column11 { get; set; }
public string Column12 { get; set; }
public string Column13 { get; set; }
public string Column14 { get; set; }
public string Column15 { get; set; }
public string Column16 { get; set; }
public string Column17 { get; set; }
public string Column18 { get; set; }
public string Column19 { get; set; }
public string Column20 { get; set; }
public string Column21 { get; set; }
public string Column22 { get; set; }
public string Column23 { get; set; }
public string Column24 { get; set; }
public string Column25 { get; set; }
public string Column26 { get; set; }
public string Column27 { get; set; }
public string Column28 { get; set; }
public string Column29 { get; set; }
public string Column30 { get; set; }
public string Column31 { get; set; }
public string Column32 { get; set; }
public string Column33 { get; set; }
public string Column34 { get; set; }
}功能要求: 声明一个该类的实体集 “List<ColumnModel> columnmodellists”,并填充值,填充规则如下。 1、声明循环变量“i”,其初始值为“0”。循环为“ColumnModel”的35个属性赋值。 2、当“i”为“0”时,为“Column0”属性赋值。 当“i”为“1”时,为“Column1”属性赋值。 当“i”为“2”时,为“Column2”属性赋值。 以此类推............................................ 当“i”为“34”时,为“Column34”属性赋值。代码片段如下: List<ColumnModel> columnmodellists = new List<ColumnModel>(); columnmodellists.Clear(); ColumnModel columnmodel = new ColumnModel(); for(int i=0; i<35 ; i++) { columnmodel.??????? = i.ToString();问题:这里的属性名应该如何指定??? 属性名也不能拼接字符串啊,怎么办??? } columnmodellists.Add(columnmodel);如何这样循环赋值???
{
public string Column0 { get; set; }
public string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }
public string Column7 { get; set; }
public string Column8 { get; set; }
public string Column9 { get; set; }
public string Column10 { get; set; }
public string Column11 { get; set; }
public string Column12 { get; set; }
public string Column13 { get; set; }
public string Column14 { get; set; }
public string Column15 { get; set; }
public string Column16 { get; set; }
public string Column17 { get; set; }
public string Column18 { get; set; }
public string Column19 { get; set; }
public string Column20 { get; set; }
public string Column21 { get; set; }
public string Column22 { get; set; }
public string Column23 { get; set; }
public string Column24 { get; set; }
public string Column25 { get; set; }
public string Column26 { get; set; }
public string Column27 { get; set; }
public string Column28 { get; set; }
public string Column29 { get; set; }
public string Column30 { get; set; }
public string Column31 { get; set; }
public string Column32 { get; set; }
public string Column33 { get; set; }
public string Column34 { get; set; }
}功能要求: 声明一个该类的实体集 “List<ColumnModel> columnmodellists”,并填充值,填充规则如下。 1、声明循环变量“i”,其初始值为“0”。循环为“ColumnModel”的35个属性赋值。 2、当“i”为“0”时,为“Column0”属性赋值。 当“i”为“1”时,为“Column1”属性赋值。 当“i”为“2”时,为“Column2”属性赋值。 以此类推............................................ 当“i”为“34”时,为“Column34”属性赋值。代码片段如下: List<ColumnModel> columnmodellists = new List<ColumnModel>(); columnmodellists.Clear(); ColumnModel columnmodel = new ColumnModel(); for(int i=0; i<35 ; i++) { columnmodel.??????? = i.ToString();问题:这里的属性名应该如何指定??? 属性名也不能拼接字符串啊,怎么办??? } columnmodellists.Add(columnmodel);如何这样循环赋值???
foreach (var info in entityProperties)
{
info.SetValue(xxx,null);
}SetValue自己查MSDN
谢谢,但是提示了错误。错误信息如下:“对象与目标类型不匹配。”
改写后的代码如下: List<ColumnModel> columnmodellists = new List<ColumnModel>();
columnmodellists.Clear();
foreach (var v in result)
{
ColumnModel columnmodel = new ColumnModel();
columnmodel.Column0 = v.DpTypeName.ToString();
//
var entityProperties = typeof(ColumnModel).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
int col_int = 1; foreach (var va in v.DpDatasArry)
{
string colname = "Column" + col_int;
entityProperties[col_int].SetValue(colname, va.ToString(), null);问题:这一句提示错误“对象与目标类型不匹配。”,怎么回事??? 应该怎样改???
}
columnmodellists.Add(columnmodel);
}
谢谢您的提示,查了一下MSDN,问题解决了。非常感激 foreach (var va in v.DpDatasArry)
{
string colname = "Column" + col_int; var colProperties = typeof(ColumnModel).GetProperty(colname); colProperties.SetValue(columnmodel, va.ToString(), null); col_int++;
}“GetProperty”和“GetProperties”这两个方法就是基于反射机制的吗???
该方法用于“Silverlight”中“DataGrid”的列绑定,List<String> ColumnList属性能绑定到列吗???
{
switch (callType)
{
case Easyteam.General.CallType.Method:
MethodInfo objMI;
objMI = ObjectRef.GetType().GetMethod(ProcName);
if (objMI == null)
{
return null;
}
return objMI.Invoke(ObjectRef, Args); case Easyteam.General.CallType.Set:
PropertyInfo objProperty;
objProperty = ObjectRef.GetType().GetProperty(ProcName);
if (objProperty == null)
{
FieldInfo field = ObjectRef.GetType().GetField(ProcName);
if (field == null)
{
return null;
}
field.SetValue(ObjectRef, Args[0]);
return null;
}
objProperty.SetValue(ObjectRef, Args[0], null);
break;
case Easyteam.General.CallType.Get:
objProperty = ObjectRef.GetType().GetProperty(ProcName);
if (objProperty == null)
{
FieldInfo field = ObjectRef.GetType().GetField(ProcName);
if (field == null)
{
return null;
}
return field.GetValue(ObjectRef); }
return objProperty.GetValue(ObjectRef, null); default:
break;
}
}
return null;
}
明白了
如果使用数组绑定到DataGrid后,会变成行,那不是行了