我是c#中使用反射的新手。我昨天发布了一个关于如何从数据表反映数据到类对象的问题。这个问题得到了回答,而且运行得很好。我现在的问题是,我有两个类的对象需要从2个不同的数据表设置。我有一个发送类和一个状态类。发送可以有多个状态,所以它是一对多关系。这是我在发送类中的变量:
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函数。
这是我的状态类中的变量:
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状态',属于发送。
我得到的结果当运行这是没有任何状态,但这里是:
<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>
希望你们能帮助我
找到了我的问题的解决方案。这就是解决办法。
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;
}