【问题标题】:Add class properties from the List and put them into the string together with a comma as a delimiter从列表中添加类属性并将它们与逗号一起作为分隔符放入字符串中
【发布时间】:2020-12-15 19:23:36
【问题描述】:

虽然我想做的事情看起来真的很微不足道,但我找不到实现我想要的方法。我知道存在多个问题,如何将类属性放在列表中并用类似 SO 的逗号分隔,但它们似乎都与我的情况无关。

我有一个类Form 定义如下:

public class Form
    {
        public string CustomerName { get; set; }
        public string CustomerAdress { get; set; }
        public string CustomerNumber { get; set; }
        public string OfficeAdress { get; set; }
        public string Date { get; set; }
        public Boolean FunctionalTest { get; set; }
        public string Signature { get; set; }


        public Form()
        {
        }
    }

MainPage.xaml.cs 中,我创建了一个带有Form 类属性的List<Form>,随后我想创建一个字符串,其中所有这些类属性用逗号分隔。对于这种情况,我使用基本的 Join 方法和 Select 将任何类型的对象转换为字符串。

我通过MainPage.xaml.cs 中的createCSV 方法做到这一点:

void createCSV()
        {    
            var records = new List<Form>
        {
            new Form {CustomerName = customerName.Text,
                CustomerAdress = customerAdress.Text,
                CustomerNumber = customerNumber.Text,
                OfficeAdress = officeAdress.Text,
                Date = date.Date.ToString("MM/dd/yyyy"),
                FunctionalTest = testPicker.ToString()=="YES" ? true : false,
                Signature = signature.Text
            }
        };


            string results = String.Join(",", (object)records.Select(o => o.ToString()));
}

问题不是理想的结果,即:"Mark Brown,123 High Level Street,01578454521,43 Falmouth Road,12/15/2020,false,Brown"

我收到:"System.Linq.Enumerable+SelectListIterator'2[MyApp.Form,System.String]"

PS。正如您所注意到的,我是 C# 的新手。请不要对代码进行非建设性的批评,而是提供有价值的回复,这将有助于我理解我做错了什么。

提前致谢

【问题讨论】:

  • 如果您想创建数据的 CSV 文件,使用 CSVHelper 可能是更好的方法
  • 是的,我也在摆弄 CSVHelper。它工作得很好,但唯一的一个问题是我必须提供一个绝对路径才能让它工作,我需要一个相对路径,我无法让它工作

标签: c# arraylist system.reflection


【解决方案1】:

Form 类中,您可以重写 ToString() 方法并使用 System.Reflection 获取逗号字符串。

Form.cs

public class Form
{
    public string CustomerName { get; set; }
    public string CustomerAdress { get; set; }
    public string CustomerNumber { get; set; }
    public string OfficeAdress { get; set; }
    public string Date { get; set; }
    public bool FunctionalTest { get; set; }
    public string Signature { get; set; }

    public override string ToString()
    {
        string modelString = string.Empty;
        PropertyInfo[] properties = typeof(Form).GetProperties();
        foreach (PropertyInfo property in properties)
        {
            var value = property.GetValue(this); // you should add a null check here before doing value.ToString as it will break on null
            modelString += value.ToString() + ",";
        }
        return modelString;
    }
}

代码

List<string> CSVDataList = new List<string>();
List<Form> FormList = new List<Form>();
...
foreach (var data in FormList)
{
    CSVDataList.Add(data.ToString());
}

现在你有一个字符串列表CSVDataList,每个Form对象的数据都是逗号样式

附言 对于日期时间

var value = property.GetValue(this);
if(value is DateTime date)
{
   modelString += date.ToString("dd.MM.yyyy") + ",";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多