Last active
August 29, 2015 14:10
-
-
Save gyuwon/cd41863e97d817d4436e to your computer and use it in GitHub Desktop.
DataTable to List<T>
This file contains 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; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Linq; | |
using System.Reflection; | |
namespace DataTableMapper | |
{ | |
public class Item | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public double Value { get; set; } | |
public override string ToString() | |
{ | |
return string.Format(@"{{ ""Id"": {0}, ""Name"": ""{1}"", ""Value"": {2} }}", Id, Name, Value); | |
} | |
} | |
public static class Program | |
{ | |
/// <summary> | |
/// <see cref="DataTable"/> κ°μ²΄λ₯Ό <see cref="List<T>"/>λ‘ λ³νν©λλ€. | |
/// </summary> | |
/// <typeparam name="T"><see cref="DataRow"/>λ₯Ό ν¬μν λμ νμμ λλ€.</typeparam> | |
/// <param name="table">μμ€ ν μ΄λΈμ λλ€.</param> | |
/// <returns><paramref name="table"/>μ λ³νν <see cref="List<T>"/>μ λλ€.</returns> | |
public static List<T> ToList<T>(DataTable table) | |
where T : new() | |
{ | |
return ToList(table, () => new T(), (c, p) => c.ColumnName == p.Name); | |
} | |
/// <summary> | |
/// <see cref="DataTable"/> κ°μ²΄λ₯Ό <see cref="List<T>"/>λ‘ λ³νν©λλ€. | |
/// </summary> | |
/// <typeparam name="T"><see cref="DataRow"/>λ₯Ό ν¬μν λμ νμμ λλ€.</typeparam> | |
/// <param name="table">μμ€ ν μ΄λΈμ λλ€.</param> | |
/// <param name="factory"><typeparamref name="T"/> κ°μ²΄λ₯Ό μμ±νλ λ©μλμ λλ€.</param> | |
/// <param name="match"><see cref="DataColumn"/>κ³Ό μμ±μ μΌμΉ μ¬λΆλ₯Ό λ°ννλ λ©μλμ λλ€.</param> | |
/// <returns><paramref name="table"/>μ λ³νν <see cref="List<T>"/>μ λλ€.</returns> | |
public static List<T> ToList<T>(DataTable table, | |
Func<T> factory, | |
Func<DataColumn, PropertyInfo, bool> match) | |
{ | |
// μ‘°νμ μ€μ μ΄ κ°λ₯ν κ³΅μ© μΈμ€ν΄μ€ μμ± λͺ©λ‘μ κ°μ Έμ΅λλ€. | |
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty); | |
// μμ± κ°μ μ€μ νλ λ©μλ λ리μλ₯Ό λΉλνλ GetSetter<,>(MethodInfo) λ©μλμ μ λ€λ¦ μ μλ₯Ό κ°μ Έμ΅λλ€. | |
// λ리μλ₯Ό λΉλνλ λͺ©μ μ μ±λ₯μ μΌλ‘ λΆλ¦¬ν 리νλ μ μμ μ μ΅μννλ κ²μ λλ€. | |
var getSetterTemplate = typeof(Program).GetMethod("GetSetter", BindingFlags.NonPublic | BindingFlags.Static); | |
// λ°μ΄ν° μ΄ κ°μ μμ±μ ν¬μνλ νλ‘μΈμ€ λͺ©λ‘μ μΊμ±ν©λλ€. | |
var setters = table.Columns | |
.Cast<DataColumn>() | |
.Select((column, index) => | |
{ | |
// columnκ³Ό μΌμΉνλ μμ±μ μ°Ύμ΅λλ€. λ μ΄μμ μμ±μ΄ μΌμΉνλ©΄ μμΈκ° λ°μν©λλ€. | |
var property = properties.SingleOrDefault(p => match(column, p)); | |
if (property != null && | |
property.PropertyType.IsAssignableFrom(column.DataType)) | |
{ | |
// columnκ³Ό μΌμΉνλ μμ±μ΄ μ‘΄μ¬νκ³ λ°μ΄ν° κ°μ μμ±μ λμ ν μ μλ κ²½μ°μ λλ€. | |
// μμ± νμμ λν GetSetter<,>(MethodInfo) μ λ€λ¦ λ©μλλ₯Ό λ§λ€μ΄ νΈμΆν©λλ€. | |
var getSetter = getSetterTemplate.MakeGenericMethod(typeof(T), property.PropertyType); | |
var setter = (Action<T, object>)getSetter.Invoke(null, new object[] { property.GetSetMethod() }); | |
// μ΄ μΈλ±μ€μ μμ± μ€μ λ©μλλ₯Ό μΊ‘μνν΄ λ°νν©λλ€. | |
return new { ColumnIndex = index, Setter = setter }; | |
} | |
return null; | |
}) | |
.Where(setter => setter != null) | |
.ToList(); | |
// DataRowλ₯Ό T νμμΌλ‘ ν¬μνλ λ©μλλ₯Ό μ μν©λλ€. | |
Func<DataRow, T> selector = row => | |
{ | |
T instance = factory(); | |
foreach (var setter in setters) | |
{ | |
var value = row[setter.ColumnIndex]; | |
setter.Setter(instance, value); | |
} | |
return instance; | |
}; | |
// tableμ List<>λ‘ λ³νν΄ λ°νν©λλ€. | |
return table.AsEnumerable().Select(selector).ToList(); | |
} | |
private static Action<TComponent, object> GetSetter<TComponent, TProperty>(MethodInfo setMethod) | |
{ | |
// μμ± μ€μ λ©μλλ₯Ό μ μ (static) ννλ‘ λΉλν©λλ€. | |
// μ μ ννλ‘ λΉλνλ©΄ 첫λ²μ§Έ 맀κ°λ³μλ‘ μΈμ€ν΄μ€λ₯Ό μ λ¬νκ² λ©λλ€. | |
var setter = (Action<TComponent, TProperty>)setMethod.CreateDelegate(typeof(Action<TComponent, TProperty>)); | |
// μΈμ€ν΄μ€μ μμ± κ°μ μ€μ νλ λ©μλλ₯Ό λ°νν©λλ€. | |
return (component, propertyValue) => setter(component, (TProperty)propertyValue); | |
} | |
public static void Main(string[] args) | |
{ | |
var table = new DataTable | |
{ | |
Columns = | |
{ | |
new DataColumn("Id", typeof(int)), | |
new DataColumn("Name", typeof(string)), | |
new DataColumn("Value", typeof(double)), | |
new DataColumn("NotMapped", typeof(string)) | |
} | |
}; | |
var row1 = table.NewRow(); | |
row1["Id"] = 1; | |
row1["Name"] = "Foo"; | |
row1["Value"] = 256; | |
row1["NotMapped"] = "Hello"; | |
table.Rows.Add(row1); | |
var row2 = table.NewRow(); | |
row2["Id"] = 2; | |
row2["Name"] = "Bar"; | |
row2["Value"] = 65536; | |
row2["NotMapped"] = "World"; | |
table.Rows.Add(row2); | |
foreach (var item in ToList<Item>(table)) | |
{ | |
Console.WriteLine(item); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: