繁体   English   中英

连接列表的属性<Class>并在 C# 中写入文件

[英]Concatenate the properties of a List<Class> and write to file in C#

我有一个定义为 List<Car> 的变量。 我想将列表中单个类的属性与一些分隔符连接起来,并写入文件而不循环属性。

例子:

    public class Car
    {
        public int CarId { get; set; }
        public string Brand { get; set; }
        public string Model { get; set; }
        public string Color { get; set; }
    }

    public void AssignData()
    {
        List<Car> cars = new List<Car>();
        cars.Add(new Car { CarId = 1, Brand = "Hundai", Model = "i10", Color = "White" });
        cars.Add(new Car { CarId = 2, Brand = "Hundai", Model = "i20", Color = "Blue" });
    }

我的预期输出将是一个以“,”作为分隔符的文本文件。

执行输出:

1,Hundai,i10,White 2,Hundai,120,Blue

我已经使用 propertyinfo 类尝试了下面的代码,但我觉得它效率不高,因为有一个嵌套循环

下面是一个尝试过的:

using (StreamWriter file = new StreamWriter(@"D:\WriteLines.txt", true))
            {
                foreach (Car car in allCars)
                {
                    PropertyInfo[] properties = car.GetType().GetProperties();
                    string fullLine = string.Empty;
                    foreach (PropertyInfo property in properties)
                    {
                        fullLine += property.GetValue(car, null).ToString() + ",";
                    }
                    file.WriteLine(fullLine);
                }
            }

好的。 让我们概括这个问题(请参阅问题的评论):让我们加入所有属性

  1. public不是static
  2. 可读可写
  3. 不是索引器(我们应该如何处理this[int, int]属性?)。
  4. 仅指定类型(例如stringintdecimaldouble )(我们如何保存,例如IComparer<int>类型的属性?)。

我们可以借助ReflectionLinq获得这些属性:

  using System.Linq;
  using System.Reflection;

  ...

  HashSet<Type> allowedTypes = new HashSet<Type>() {
    typeof(string), typeof(int), typeof(decimal), typeof(double)
  };

  var properties = typeof(Car)
    .GetProperties(BindingFlags.Instance | BindingFlags.Public)
    .Where(prop => prop.CanRead && prop.CanWrite)
    .Where(prop => !prop.GetIndexParameters().Any())
    .Where(prop => allowedTypes.Contains(prop.PropertyType))
 // .Where(prop => ...) // Add here more coditions if required
    .ToArray();

现在,有了这些properties我们可以将值保存到文件中:

  using System.IO;
  using System.Linq;

  ...

  File.WriteAllLines(@"D:\WriteLines.txt", cars
    .Select(car => string.Join(",", properties
       .Select(property => property.GetValue(car)))));

最后,您可以将这些部分组合成一个例程

  private static void SaveToFile<T>(IEnumerable<T> source, string fileName) {
    HashSet<Type> allowedTypes = new HashSet<Type>() {
      typeof(string), typeof(int), typeof(decimal), typeof(double)
    };

    var properties = typeof(T)
      .GetProperties(BindingFlags.Instance | BindingFlags.Public)
      .Where(prop => prop.CanRead && prop.CanWrite)
      .Where(prop => !prop.GetIndexParameters().Any())
      .Where(prop => allowedTypes.Contains(prop.PropertyType))
   // .Where(prop => ...) // Add here more coditions if required
      .ToArray();

    File.WriteAllLines(fileName, source
      .Select(item => string.Join(",", properties
        .Select(property => property.GetValue(item))))); 
  } 

然后使用它:

  List<Car> cars = new List<Car>();

  ... 

  SaveToFile(Cars, @"D:\WriteLines.txt");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM