从asp.net传递控制台应用程序中的常规列表
我有一个解决方案,我需要从asp.net调用控制台应用程序,并需要传递变量。一个变量是某个类的泛型列表 我试图传递它,但我得到了一个错误,我不能将一个通用列表转换为一个正确的字符串 我不确定是否还有其他方法可以通过 我知道webservice可以解决这个问题。但还有其他选择吗 这是可以做到的还是只有字符串可以通过从asp.net传递控制台应用程序中的常规列表,asp.net,console-application,Asp.net,Console Application,我有一个解决方案,我需要从asp.net调用控制台应用程序,并需要传递变量。一个变量是某个类的泛型列表 我试图传递它,但我得到了一个错误,我不能将一个通用列表转换为一个正确的字符串 我不确定是否还有其他方法可以通过 我知道webservice可以解决这个问题。但还有其他选择吗 这是可以做到的还是只有字符串可以通过 Here is the generic list sample. List<person> personList = new List<person>();
Here is the 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)
以下是通用列表示例。
List personList=新列表();
人员p=新人员();
p、 name=“测试”;
p、 年龄=12岁;
p、 出生日期=2014年1月1日
个人列表。添加(p)
谢谢。好的,控制台应用程序只接受字符串。这在
Main
方法中定义为
static void Main(string[] args)
因为您有一个复杂的对象列表,所以将此信息传递给控制台应用程序有点困难(但并非不可能)。你有几个选择
webservices
或您建议的webapi
将对象序列化为XML文件,然后在控制台应用程序中进行反序列化
Default.aspx
),在它的单击事件中,它将个人集合/列表写入XML文件。这是背后的代码
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();
}
}
}
而且,Person.cs
看起来像这样
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;
}
}
}
在控制台应用程序中,您应该具有与模态相同的Person类(这与Web应用程序中的Person类相同。只是名称空间不同)
使用系统;
使用System.Collections.Generic;
命名空间ReadInCole
{
[可序列化]
[System.Xml.Serialization.XmlRoot(“PersonCollection”)]
公共类PersonCollection:列表
{
}
[可序列化]
公共阶层人士
{
公共字符串名称{get;set;}
公共整数{get;set;}
公共日期时间出生日期{get;set;}
公众人士()
{
this.Name=string.Empty;
这个。年龄=0;
this.BirthDate=DateTime.MinValue;
}
}
}
希望您理解代码。您的通用列表中有什么?显示一些代码samples@sam我更新了问题。感谢您的关注,我已经尝试过以xml形式传递。但是这些元素被标记为不同的args。假设我通过了一个人的测试,你能告诉我你是怎么做到的吗?然后,我可能能够解决以下问题:document=newxdocument(new-XElement(“PersonInfo”,new-XElement(“Persons”),personlist.Select(x=>new-XElement(“Person”,new-XAttribute(“Name”,x.Name()()));嗨,山姆,谢谢你。现在我将尝试我的解决方案如果您试图在两个应用程序上下文之间共享相同的内存(将XML保留在内存中),那么这将是一个不同的解决方案。
using System;
using System.IO;
using System.Xml.Serialization;
namespace ReadInConsole
{
class Program
{
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} and DOB: {2}", p.Name, p.Age, p.BirthDate.ToShortDateString());
}
Console.ReadLine();
}
}
}
}
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;
}
}
}