Pass generic list in console app from asp.net -
Pass generic list in console app from asp.net -
i have solution need phone call console app asp.net , need pass variables. 1 variable generic list of class.
i have tried passing got error cannot convert generic list string correct.
i not sure if there way pass this.
i know webservice can solve issue. there other options?
is possible or string possible pass
here generic list sample. list<person> personlist = new list<person>(); person p = new person(); p.name = "test"; p.age = 12; p.birthdate = 01/01/2014 personlist.add(p)
thanks.
ok, console application accepts strings. defined in main
method
static void main(string[] args)
since have complex object list it'll bit hard pass info console application (but not impossible). there several options you.
pass values comma separated values string long string not long.web services
or web api
suggested. serialize
object xml file , deserialize
in console application. write , read persistent info store update
sample code alternative 3 (write xml file)
i wrote sample code out of curiosity. hope helps solve issue.
asp.net website
i have button in web page (default.aspx
) , in it's click event writes person collection/ list xml file. here's code behind.
using system; using system.io; using system.xml.serialization; namespace writetoconsole { public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void btnwritetoconsole_click(object sender, eventargs e) { personcollection personlist = new personcollection(); // person 1 person p = new person(); p.name = "test 1"; p.age = 12; p.birthdate = datetime.parse("01/01/2014"); personlist.add(p); // person 2 person p2 = new person(); p2.name = "test 2"; p2.age = 25; p2.birthdate = datetime.parse("01/01/2014"); personlist.add(p2); xmlserializer serializer = new xmlserializer(personlist.gettype()); streamwriter file = new streamwriter(@"d:\temp\personcollection.xml"); serializer.serialize(file, personlist); file.close(); } } }
and, person.cs
looks this.
using system; using system.collections.generic; namespace writetoconsole { [serializable] [system.xml.serialization.xmlroot("personcollection")] public class personcollection : list<person> { } [serializable] public class person { public string name { get; set; } public int age { get; set; } public datetime birthdate { get; set; } public person() { this.name = string.empty; this.age = 0; this.birthdate = datetime.minvalue; } } }
console application
then read xml file in console application , display info in personlist
on console.
using system; using system.io; using system.xml.serialization; namespace readinconsole { class programme { static void main(string[] args) { xmlserializer deserializer = new xmlserializer(typeof(personcollection)); textreader textreader = new streamreader(@"d:\temp\personcollection.xml"); personcollection personlist = new personcollection(); personlist = (personcollection)deserializer.deserialize(textreader); textreader.close(); if (personlist != null && personlist.count > 0) { foreach (person p in personlist) { console.writeline("person name: {0}, age: {1} , dob: {2}", p.name, p.age, p.birthdate.toshortdatestring()); } console.readline(); } } } }
in console application should have same person class modal (this same person class in web application. namespace different).
using system; using system.collections.generic; namespace readinconsole { [serializable] [system.xml.serialization.xmlroot("personcollection")] public class personcollection : list<person> { } [serializable] public class person { public string name { get; set; } public int age { get; set; } public datetime birthdate { get; set; } public person() { this.name = string.empty; this.age = 0; this.birthdate = datetime.minvalue; } } }
hope understand code.
asp.net console-application
Comments
Post a Comment