我是C#中使用Reflections的新手。我昨天发布了一个关于如何将数据从数据表反映到类对象的问题。这个问题得到了回答,并且完美无缺。我现在的问题是我有两个类,需要从2个不同的数据表中设置对象。我有一个发送类和一个Statusclass。发送可以有很多状态'所以它是一对多的关系。这是我在发送类中的变量:
private string _Barcode;
private string _Barcode2;
private DateTime _PickupDate;
private string _PickupCustomer;
private string _PickupName;
private string _PickupStreet;
private string _PickupHouseNo;
private string _PickupPostal;
private string _PickupCity;
private string _PickupCountry;
private DateTime _DeliveryDate;
private string _DeliveryCustomer;
private string _DeliveryName;
private string _DeliveryStreet;
private string _DeliveryHouseNo;
private string _DeliveryPostal;
private string _DeliveryCity;
private string _DeliveryCountry;
private int _Coli;
private double _Weight;
private double _Volumen;
private List<StatusHistory> _StatusHistory = new List<StatusHistory>();
每个变量都有一个get和set函数。
这是我的Status类中的变量:
private int _SendingID;
private string _DanxCode;
private string _RecpName;
private int _Logtime;
这是我将数据从数据表反映到类对象的方法:
public static IList<Sending> ReflectData<Sending>(DataTable sendingList, DataTable statusList)
{
List<Sending> list = Activator.CreateInstance<List<Sending>>();
Sending sendingInstance = Activator.CreateInstance<Sending>();
var property = sendingInstance.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (DataRow dr in sendingList.Rows)
{
Sending ins = Activator.CreateInstance<Sending>();
foreach (var p in property)
{
try
{
p.SetValue(ins, dr[p.Name], null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
DataRow[] row = statusList.Select("SendingID = " + dr[0]);
List<Status> sh = Activator.CreateInstance<List<Status>>();
foreach (DataRow r in row)
{
Status statusInstance = Activator.CreateInstance<Status>();
var porpsh = statusInstance.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var psh in porpsh)
{
try
{
psh.SetValue(statusInstance, r[psh.Name], null);
}
catch { }
}
}
list.Add(ins);
}
return list;
}
我需要我的结果包含一个发送(发货)的相关4状态&#39;属于发送。
我在运行时得到的结果是没有任何状态,但这里是:
<ArrayOfSending xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://danx.com/">
<Sending>
<Barcode>SE140</Barcode>
<Barcode2>SE140</Barcode2>
<PickupDate>2014-02-13T00:00:00</PickupDate>
<PickupName>Parts Europe</PickupName>
<PickupStreet>xxxx</PickupStreet>
<PickupHouseNo>1</PickupHouseNo>
<PickupPostal>xxxxx</PickupPostal>
<PickupCity>xxxxx</PickupCity>
<PickupCountry>xxxx</PickupCountry>
<DeliveryDate>2014-03-18T12:00:00</DeliveryDate>
<DeliveryName>Bil AB</DeliveryName>
<DeliveryStreet>zzzzz</DeliveryStreet>
<DeliveryHouseNo>39</DeliveryHouseNo>
<DeliveryPostal>zzzzz</DeliveryPostal>
<DeliveryCity>zzzzz</DeliveryCity>
<DeliveryCountry>zzzzzz</DeliveryCountry>
<Coli>1</Coli>
<Weight>1</Weight>
<Volumen>0.05</Volumen>
<StatusHistory/>
</Sending>
</ArrayOfSending>
希望你们能帮帮我。
答案 0 :(得分:0)
找到我的问题的解决方案。这是解决方案。
public static IList<Sending> ReflectData(DataTable sendingList, DataTable statusList)
{
List<Sending> list = Activator.CreateInstance<List<Sending>>();
var property = typeof(Sending).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (DataRow dr in sendingList.Rows)
{
Sending sendingInstance = Activator.CreateInstance<Sending>();
foreach (var p in property)
{
try
{
p.SetValue(sendingInstance, dr[p.Name], null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
DataRow[] row = statusList.Select("SendingID = " + dr[0]);
foreach (DataRow r in row)
{
StatusHistory statusInstance = Activator.CreateInstance<StatusHistory>();
var porpsh = statusInstance.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var psh in porpsh)
{
try
{
psh.SetValue(statusInstance, r[psh.Name], null);
}
catch { }
}
sendingInstance.StatusHistory.Add(statusInstance);
}
list.Add(sendingInstance);
}
return list;
}