将泛型集合转换为字符串列表

时间:2022-02-19 16:05:36

After searching the board, i was not able to find a solution to my problem.

搜索完董事会后,我无法找到问题的解决方案。

I have written the following code that works pretty well:

我编写了以下代码,效果很好:

public static  void CreateFile(this List<string> lines,File_attribute fa)
{
     using (System.IO.StreamWriter file = new System.IO.StreamWriter(fa.OutpoutFolder+ fa.FileName ))
     {
         foreach (string line in lines)
         {

             file.WriteLine(line);

         }
     }
}

Now, i want to be able to do just this with a generic collection of object. I want each property to just be "ToStringed". Here is the start of my code:

现在,我希望能够通过一个通用的对象集合来做到这一点。我希望每个房产都是“ToStringed”。这是我的代码的开头:

public static void CreateFile<T>(this List<T> lines, File_attribute fa)
{
    List<string> mylist = new List<string>();
    ...............
    ...............

Any help will be highly appreciated.

任何帮助将受到高度赞赏。

5 个解决方案

#1


1  

Here's a possible alternate approach using string.Join:

这是使用string.Join的一种可能的替代方法:

public static void CreateFile<T>(this List<T> lines, File_attribute fa)
{
     File.WriteAllText
     (
          Path.Combine(fa.OutpoutFolder, fa.FileName),
          string.Join(Environment.NewLine, lines)
     );
}

So string.Join will internally call Object.ToString()!

所以string.Join会在内部调用Object.ToString()!

#2


2  

You can use LINQ for this

您可以使用LINQ

public static void CreateFile<T>(this List<T> lines, File_attribute fa)
{
    List<string> mylist = lines.Select(x => x.ToString()).ToList();
    ...............
    ...............

#3


1  

Simply use ToString for every item, the rest can stay the same:

只需对每个项目使用ToString,其余的可以保持不变:

public static void CreateFile<T>(this List<T> items, File_attribute fa)
{
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(fa.OutpoutFolder + fa.FileName))
    {
        foreach (T item in items)
            file.WriteLine(item.ToString());
    }
}

By doing it this way, for a huge list this will prevent your application from allocating a lot of memory before it can write anything to disk.

通过这种方式,对于一个巨大的列表,这将阻止您的应用程序在可以将任何内容写入磁盘之前分配大量内存。

#4


0  

public static void CreateFile<T>(this List<T> lines)
{
    List<string> mylist = new List<string>();
    var props = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
    foreach (var item in lines)
    {
        foreach (var prop in props)
        {
            var val = prop.GetValue(item);
            // do what you want with val, you can call ToString() on it
        }
    }
}

#5


0  

Although everything derives from System.Object and therefor implements .ToString() it is very easy to overlook this as a caller and use it on a type where you did not overwrite .ToString() which would then output the objects type instead of a some expected message. For better control I would highly recommend an interface and then use that as a generic constraint on your method.

虽然一切都源自System.Object,因此实现.ToString()很容易忽略它作为调用者并在你没有覆盖的类型上使用它.ToString()然后输出对象类型而不是一些预期的消息。为了更好地控制,我强烈推荐一个接口,然后将其用作方法的通用约束。

public interface ILine {
    string GetLineOutput();
}

Then in your class.

然后在你的班上。

public static  void CreateFile(this List<T> lines, File_attribute fa) where T : ILine
{
     using (System.IO.StreamWriter file = new System.IO.StreamWriter(System.IO.Path.Combine(fa.OutpoutFolder, fa.FileName)))
     {
         foreach (ILine line in lines)
         {
             file.WriteLine(lin.GetLineOutput());
        }
     }
}

Some side notes

一些旁注

  1. When creating a file path with variable parts you should use System.IO.Path.Combine to combine these parts.
  2. 在创建包含可变部分的文件路径时,应使用System.IO.Path.Combine来组合这些部分。
  3. Good job on using a stream and also writing to it line by line instead of all at once which could cause memory issues. Also good job on using using for your stream!
  4. 很好地使用流并逐行写入,而不是一次性写入,这可能会导致内存问题。使用你的流也很好!

#1


1  

Here's a possible alternate approach using string.Join:

这是使用string.Join的一种可能的替代方法:

public static void CreateFile<T>(this List<T> lines, File_attribute fa)
{
     File.WriteAllText
     (
          Path.Combine(fa.OutpoutFolder, fa.FileName),
          string.Join(Environment.NewLine, lines)
     );
}

So string.Join will internally call Object.ToString()!

所以string.Join会在内部调用Object.ToString()!

#2


2  

You can use LINQ for this

您可以使用LINQ

public static void CreateFile<T>(this List<T> lines, File_attribute fa)
{
    List<string> mylist = lines.Select(x => x.ToString()).ToList();
    ...............
    ...............

#3


1  

Simply use ToString for every item, the rest can stay the same:

只需对每个项目使用ToString,其余的可以保持不变:

public static void CreateFile<T>(this List<T> items, File_attribute fa)
{
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(fa.OutpoutFolder + fa.FileName))
    {
        foreach (T item in items)
            file.WriteLine(item.ToString());
    }
}

By doing it this way, for a huge list this will prevent your application from allocating a lot of memory before it can write anything to disk.

通过这种方式,对于一个巨大的列表,这将阻止您的应用程序在可以将任何内容写入磁盘之前分配大量内存。

#4


0  

public static void CreateFile<T>(this List<T> lines)
{
    List<string> mylist = new List<string>();
    var props = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
    foreach (var item in lines)
    {
        foreach (var prop in props)
        {
            var val = prop.GetValue(item);
            // do what you want with val, you can call ToString() on it
        }
    }
}

#5


0  

Although everything derives from System.Object and therefor implements .ToString() it is very easy to overlook this as a caller and use it on a type where you did not overwrite .ToString() which would then output the objects type instead of a some expected message. For better control I would highly recommend an interface and then use that as a generic constraint on your method.

虽然一切都源自System.Object,因此实现.ToString()很容易忽略它作为调用者并在你没有覆盖的类型上使用它.ToString()然后输出对象类型而不是一些预期的消息。为了更好地控制,我强烈推荐一个接口,然后将其用作方法的通用约束。

public interface ILine {
    string GetLineOutput();
}

Then in your class.

然后在你的班上。

public static  void CreateFile(this List<T> lines, File_attribute fa) where T : ILine
{
     using (System.IO.StreamWriter file = new System.IO.StreamWriter(System.IO.Path.Combine(fa.OutpoutFolder, fa.FileName)))
     {
         foreach (ILine line in lines)
         {
             file.WriteLine(lin.GetLineOutput());
        }
     }
}

Some side notes

一些旁注

  1. When creating a file path with variable parts you should use System.IO.Path.Combine to combine these parts.
  2. 在创建包含可变部分的文件路径时,应使用System.IO.Path.Combine来组合这些部分。
  3. Good job on using a stream and also writing to it line by line instead of all at once which could cause memory issues. Also good job on using using for your stream!
  4. 很好地使用流并逐行写入,而不是一次性写入,这可能会导致内存问题。使用你的流也很好!