0

I'm trying to create a .dat file with a pipe (|) separator. I'm not sure how I can set custom column names I can use in a .dat file. I have a class that would look like this :

public partial class ExportData
{
    [DatColumnName="Test Code 123"]
    public string Code { get; set; }
    [DatColumnName="Test Name 123"]
    public string Name { get; set; }
    [DatColumnName="Test Address_123"]
    public string Address { get; set; }
}

As a result, I would like to get something like this in the file:

Test Code 123|Test Name 123|Test Address_123
1|Name1|Address1
etc.
azekirel555
  • 577
  • 2
  • 8
  • 25
  • What have you tried already? I can recommend you look up [reflection](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection) as it can probably do what you want (and more) – MindSwipe May 18 '20 at 12:49
  • I could probably whip something up, but I'm not going to. Because StackOverflow is not a code writing service. I am however happy to help if you run into any specific problem you need help with and cannot find an answer anywhere else – MindSwipe May 18 '20 at 13:07
  • 1
    It's a well known problem, first hit I got was on here https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property should give you a starter. – Tony Hopkinson May 18 '20 at 13:23

1 Answers1

3

I guess below code will works for you. I could handle it without custom attribute.

Code

public static class Extensions
{
    public static string Piped<T>(this IList<T> source)
    {
        var sb = new StringBuilder();

        var pInfo = typeof(T).GetProperties().Where(x => x.GetCustomAttributes<DisplayNameAttribute>().Any());

        bool first = true;

        foreach (var i in source)
        {
            if (first)
            {
                //foreach (var h in pInfo.Select(x => x.GetCustomAttribute<DisplayNameAttribute>().DisplayName))
                //{
                //    sb.Append(h);
                //    sb.Append('|');
                //}

                sb.Append(string.Join('|', pInfo.Select(x => x.GetCustomAttribute<DisplayNameAttribute>().DisplayName)));

                sb.Append(Environment.NewLine);

                first = false;
            }

            foreach (var y in i.GetType().GetProperties().Where(x => x.GetCustomAttributes<DisplayNameAttribute>().Any()))
            {
                sb.Append(i.GetType().GetProperty(y.Name).GetValue(i, null).ToString());
                sb.Append('|');
            }

            sb.Append(Environment.NewLine);
        }

        return sb.ToString();
    }
}

Usage

public partial class ExportData
    {
        [DisplayName("Test Code 123")]
        public string Code { get; set; }
        [DisplayName("Test Name 123")]
        public string Name { get; set; }
        [DisplayName("whatever you want")]
        public string Address { get; set; }
    }

 static void Main(string[] args)
    {

        var lst = new List<ExportData>() {

        new ExportData{ Code = "c1", Name ="n1", Address = "a1" },
        new ExportData{ Code = "c2", Name ="n2", Address = "a2" },
        new ExportData{ Code = "c3", Name ="n3", Address = "a3" },

        };


        Console.WriteLine(lst.Piped());
        Console.ReadKey();
    }

Result

Test Code 123|Test Name 123|Whatever you want|
c1|n1|a1|
c2|n2|a2|
c3|n3|a3|
anilcemsimsek
  • 803
  • 10
  • 21