-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.cs
49 lines (47 loc) · 2.05 KB
/
Converter.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace Eonae.CollectionExtensions
{
/// 1. Может быть попробовать сделать обратную операцию?? С генерацией нового класса.
/// 2. В конце концов сгенерировать новый файл с расширение cs проблем никаких нет...
public static class TableConverter
{
public static DataTable ToDataTable<SomeClass>(this IEnumerable<SomeClass> list, string name)
{
DataTable table = new DataTable() { TableName = name };
Type type = typeof(SomeClass);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
var memberTypes = new Dictionary<string, MemberTypes>();
foreach (var field in fields)
{
memberTypes.Add(field.Name, MemberTypes.Field);
table.Columns.Add(field.Name);
}
foreach (var property in properties)
{
memberTypes.Add(property.Name, MemberTypes.Property);
table.Columns.Add(property.Name);
}
foreach (SomeClass item in list)
{
var values = new object[table.Columns.Count];
for (int i = 0; i < table.Columns.Count; i++)
{
string memberName = table.Columns[i].ColumnName;
switch (memberTypes[memberName])
{
case MemberTypes.Field:
values[i] = type.GetField(memberName).GetValue(item); break;
case MemberTypes.Property:
values[i] = type.GetProperty(memberName).GetValue(item); break;
}
}
table.Rows.Add(values);
}
return table;
}
}
}