Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - How to use EnumConverter with CsvHelper

I'm using CsvHelper to serialize a class to csv file - until here everything works well.

Now I'm trying to find a way to convert the class's enum properties to their int value in the csv, so I could use the CSV for bulk insert later.

I found out the EnumConverter class in CsvHelper but I can't figure out how to properly use it, as all my tries are failing.

Here is my mapping class code

 public sealed class MyMapping : CsvClassMap<TradingCalendarException>
    {
        public MyMapping()
        {
            EnumConverter enumConverter = new EnumConverter(typeof(CalendarExceptionEntityType));

            Map(m => m.ExceptionEntityType).Index(0).Name("EXCEPTION_ENTITY_TYPE").TypeConverter(enumConverter);
            Map(m => m.ExceptionEntityIdentifier).Index(1).Name("EXCEPTION_ENTITY_IDENTIFIER");
            Map(m => m.OptionType).Index(2).Name("OPTION_TYPE");
            Map(m => m.StartDatetime).Index(3).Name("EXCEPTION_START_DATETIME");
            Map(m => m.EndDatetime).Index(4).Name("EXCEPTION_END_DATETIME");
            Map(m => m.DataSourceType).Index(5).Name("DATA_SOURCE_TYPE");
            Map(m => m.Description).Index(6).Name("DESCRIPTION");
        }
    }

and the writing part

using (StreamWriter file = new StreamWriter(filePath, false, Encoding.UTF8))
        {
            CsvWriter writer = new CsvWriter(file);
            MyMapping mapping = new MyMapping();
            writer.Configuration.RegisterClassMap(mapping);

            writer.WriteRecords(calendarExceptionList);
        }

The rest of the mapping (indexing and naming) is working, it's just the EnumConverter that doesn't do any change.

I didn't find any examples online.

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the solution I made:

public class CalendarExceptionEnumConverter<T> : DefaultTypeConverter  where T : struct
    {
        public override string ConvertToString(TypeConverterOptions options, object value)
        {
            T result;
            if(Enum.TryParse<T>(value.ToString(),out result))
            {
                return (Convert.ToInt32(result)).ToString();
            }

            throw new InvalidCastException(String.Format("Invalid value to EnumConverter. Type: {0} Value: {1}",typeof(T),value));
        }
    }

and used it as the following:

Map(m => m.ExceptionEntityType).TypeConverter<CalendarExceptionEnumConverter<CalendarExceptionEntityType>>();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...