c# - List<CustomClass> sent as List<T>; how to get the properties? -
i have piece of code
public class ticket { public string strarticleid { get; set; } public string strarticledescription { get; set; } public decimal decarticleprice { get; set; } public decimal decarticlevat { get; set; } public decimal decarticulenetprice { get; set; } public decimal decarticlediscount { get; set; } public decimal decarticlequantity { get; set; } } public static list<ticket> _lstcurrentticket = new list<ticket>();
that want send external dll lines in _lstcurrentticket print ticket through
for (int = 0; < datagridview1.rows.count; i++) { ticket ticket = new ticket(); string strrefid = this.datagridview1.rows[i].cells[0].value.tostring(); string strdescription = this.datagridview1.rows[i].cells[1].value.tostring(); decimal decquantity = (decimal)this.datagridview1.rows[i].cells[2].value; decimal decuprice = (decimal)this.datagridview1.rows[i].cells[3].value; decimal decdiscount = convert.todecimal(this.datagridview1.rows[i].cells[4].value.tostring().substring(0, this.datagridview1.rows[i].cells[4].value.tostring().length - 1)); decimal decvat = convert.todecimal(this.datagridview1.rows[i].cells[5].value.tostring().substring(0, this.datagridview1.rows[i].cells[5].value.tostring().length - 1)); decimal decgprice = (decimal)this.datagridview1.rows[i].cells[6].value; ticket.strarticleid = strrefid; ticket.strarticledescription = strdescription; ticket.decarticleprice = decuprice; ticket.decarticlevat = decvat; ticket.decarticulenetprice = decgprice; ticket.decarticlediscount = decdiscount; ticket.decarticlequantity = decquantity; _lstcurrentticket.add(ticket); } ticketprinting tktprint = new ticketprinting (); //ticket , copies tktprint.printticketfromlist(_lstcurrentticket, 2);
since external dll, thought easiest way work in target dll was
public void printticketfromlist<t>(list<t> lstarticles, short intcopies) { foreach (var prop in lstarticles.gettype().getproperties()) { if (prop.name == "item") { //copy external list local class printing } }...
but i'm stuck there. how can iterate each property , value each original class in list can copy it? if make breakpoint can see fields , values correctly passed, not how access them can creating local class original , clone list (and if try local list(ticket) , passed list(t) not same type).
or how copy if create exact class in target , like
public void printticketfromlist(object lstarticles, short intcopies) { list<targetdllticket> lst =((list<targetdllticket>)lstarticles).tolist(); }
any thoughts?
if @ point possible should try avoid reflection igor in answer.
but if want use reflection not inspecting item list of items.
you should try (writing memory):
public void printticketfromlist<t>(list<t> lstarticles, short intcopies) { foreach (var item in lstarticles) { foreach (var prop in typeof(t).getproperties()) { var value = prop.getvalue(item); } } }
Comments
Post a Comment