我有这段代码:
public class TABLE01
{
public string FIELD1 { get; set; }
public string FIELD2 { get; set; }
}
...
// fieldlist:
List<string> fieldsTABLE01 = new List<string>();
fieldsTABLE01.Add("FIELD1");
fieldsTABLE01.Add("FIELD2");
//response type List:
lResponseLegacy.Clear();
lResponseLegacy = DataExtract(fieldsTABLE01);
List<ClassDefinitions.TABLE01> listDataTableTABLE01 = new List<ClassDefinitions.TABLE01>();
foreach (string linea in lResponseLegacy)
{
ClassDefinitions.TABLE01 tableTABLE01 = new ClassDefinitions.TABLE01();
tableTABLE01.table01FIELD1 = ClassPackSupport.GetStringBetween(linea, "<FIELD1>", "</FIELD1>");
tableTABLE01.table01FIELD2 = ClassPackSupport.GetStringBetween(linea, "<FIELD2>", "</FIELD2>");
listDataTableTABLE01.Add(tableTABLE01);
}
classRespuestaDataclassModelo.tableTABLE01 = listDataTableTABLE01;
现在,我需要使用与fieldsTABLE01
等其他表相同的先前代码和自己的字段。同样,前面的代码必须从类的动态定义(ClassDefinitions.TABLE01
)创建一个列表,并在这些类上循环它们自己的字段。
换句话说,我需要为其他类使用相同的代码(可能在方法上):
public class TABLE02
{
public string FIELD3 { get; set; }
public string FIELD4 { get; set; }
public string FIELD9 { get; set; }
}
public class TABLE03
{
public string FIELD5 { get; set; }
public string FIELD6 { get; set; }
}
public class TABLE04
{
public string FIELD7 { get; set; }
public string FIELD8 { get; set; }
public string FIELD14 { get; set; }
public string FIELD15 { get; set; }
}
拜托,你能给我一些提示吗?
感谢。
答案 0 :(得分:0)
您可以使用这样的反射
//Get a classes properties, you can do this with many classes.
var yourClassProperties = typeof(YourClass).GetProperties();
//And down the line:
fieldsTABLE01 = fieldsTABLE01.Concat(yourClassProperties).ToList();