-
-
Save maximgorbatyuk/e5808788f94e90ecb400b86b72fc7818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Serializer | |
| { | |
| public class F { | |
| public int i1 { get; set; } | |
| public int i2 { get; set; } | |
| public int i3 { get; set; } | |
| public int i4 { get; set; } | |
| public int i5 { get; set; } | |
| public F(int i) { i1 = i; i2 = i + 1; i3 = i + 2; i4 = i + 3; i5 = i + 4; } | |
| public F() { } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.ComponentModel; | |
| using System.Linq; | |
| using System.Text; | |
| namespace Serializer | |
| { | |
| static class SerializerExtentions | |
| { | |
| public static string ToCsv(this object obj, string separator) | |
| { | |
| var fields = obj.GetType().GetProperties(); | |
| StringBuilder line = new StringBuilder(); | |
| if (obj is string) | |
| { | |
| line.Append(obj as string); | |
| return line.ToString(); | |
| } | |
| foreach (var field in fields) | |
| { | |
| var value = field.GetValue(obj); | |
| var name = field.Name; | |
| var fieldType = field.GetValue(obj).GetType(); | |
| if (line.Length > 0) | |
| { | |
| line.Append(separator); | |
| } | |
| line.Append(name.ToString()); line.Append(":"); | |
| if (value is string) { | |
| line.Append(value as string); | |
| } | |
| if (typeof(IEnumerable).IsAssignableFrom(fieldType)) { | |
| var objectList = value as IEnumerable; | |
| StringBuilder row = new StringBuilder(); | |
| foreach (var item in objectList) { | |
| if (row.Length > 0) { | |
| row.Append(separator); | |
| } | |
| row.Append(item.ToCsv(separator)); | |
| } | |
| line.Append(row.ToString()); | |
| } | |
| else | |
| { | |
| line.Append(value.ToString()); | |
| } | |
| } | |
| return line.ToString(); | |
| } | |
| public static object FromCSV<T>(this string csv, string separator) where T : new() | |
| { | |
| var parts = csv.Split(separator); | |
| var datum = new T(); | |
| var fields = datum.GetType().GetProperties(); | |
| for (int i = 0; i < parts.Length; i++) { | |
| var token = parts[i].Split(":"); | |
| var property = token[0]; | |
| var value = token[1]; | |
| var p = fields.FirstOrDefault(a => a.Name.Equals(property)); | |
| var converter = TypeDescriptor.GetConverter(p.PropertyType); | |
| var convertedvalue = converter.ConvertFrom(value); | |
| p.SetValue(datum, convertedvalue); | |
| } | |
| return datum; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment