0
我是在C#中使用反射的新手段。我昨天發佈了一個關於如何將數據表中的數據反映到類對象的問題。這個問題得到了回答,它的工作完美。我現在的問題是我有兩個類需要從2個不同的數據表中設置對象。我有一個發送類和一個狀態類。發送可以有很多狀態,所以它是一對多的關係。這是我在發送類中的變量:將數據表示數據反映到類對象中C#
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>
希望你們能幫助我。
您可能還想要添加對Nullable <>類型的檢查,因爲如果類型爲Nullable,則Property.SetValue將失敗。我寫了一個DataTable到Class並返回類。我也使用泛型,並可以從DataTable構建SQL腳本。它喜歡將對象存儲爲DataTable,並將其轉換爲任何對象。隨意複製我的代碼,如你所願。 [csharpprogramming.tips/2015/01/entity-justworks-class-to-sql.html](http://www.csharpprogramming.tips/2015/01/entity-justworks-class-to-sql.html)。 –