-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvConvert.cs
More file actions
60 lines (53 loc) · 2.09 KB
/
CsvConvert.cs
File metadata and controls
60 lines (53 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using DEXS.IO.CSV.Attributes;
namespace DEXS.IO.CSV
{
public class CsvConvert
{
public static string SerializeObject(object o, CsvFormatOptions options)
{
return SerializeObject(o, null, options);
}
public static string SerializeObject(object o, Type type, CsvFormatOptions options)
{
// var serializer = new CsvSerializer();
return "";
}
public static IEnumerable<T> DeserializeString<T>(string data) where T : class, new()
{
var serializer = new CsvSerializer<T>();
var stream = new StreamReader(GenerateStreamFromString(data));
var x = serializer.Deserialize(stream);
return x;
}
public static IEnumerable<T> DeserializeFromStream<T>(StreamReader data) where T : class, new()
{
var serializer = new CsvSerializer<T>();
var x = serializer.Deserialize(data);
return x;
}
public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
public static IEnumerable<CsvProperty> GetCSVProperties(Type type)
{
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance
| BindingFlags.GetProperty | BindingFlags.SetProperty);
return (from prop in properties
where prop.GetCustomAttribute<CsvIgnoreAttribute>() == null
let csvColumnAttribute = (CsvColumnAttribute)Attribute.GetCustomAttribute(prop, typeof(CsvColumnAttribute)) ?? new CsvColumnAttribute()
orderby csvColumnAttribute.Order, csvColumnAttribute.Name ?? prop.Name
select new CsvProperty(prop, csvColumnAttribute)).ToList();
}
}
}