是否有一種很好的方法將IEnDigable轉(zhuǎn)換為DataTable?我可以使用反射來獲取屬性和值,但這似乎有點低效,有內(nèi)置的東西嗎?(我知道這樣的例子:ObtainDataTableFromIEnDigable)編輯:這,這個問題通知我處理空值有問題。下面我編寫的代碼正確地處理空值。public static DataTable ToDataTable<T>(this IEnumerable<T> items) {
// Create the result table, and gather all properties of a T
DataTable table = new DataTable(typeof(T).Name);
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Add the properties as columns to the datatable
foreach (var prop in props) {
Type propType = prop.PropertyType;
// Is it a nullable type? Get the underlying type
if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
propType = new NullableConverter(propType).UnderlyingType;
table.Columns.Add(prop.Name, propType);
}
// Add the property values per T as rows to the datatable
foreach (var item in items) {
var values = new object[props.Length];
for (var i = 0; i < props.Length; i++)
values[i] = props[i].GetValue(item, null);
table.Rows.Add(values);
}
return table; }
3 回答

LEATH
TA貢獻(xiàn)1936條經(jīng)驗 獲得超7個贊
where T:class
GetValue
ref
.
GetValue
MethodInfo method = property.GetGetMethod(true);Delegate.CreateDelegate(typeof(Func<TClass, TProperty>), method );
TProperty
public class ReflectionUtility{ internal static Func<object, object> GetGetter(PropertyInfo property) { // get the get method for the property MethodInfo method = property.GetGetMethod(true); // get the generic get-method generator (ReflectionUtility.GetSetterHelper<TTarget, TValue>) MethodInfo genericHelper = typeof(ReflectionUtility).GetMethod( "GetGetterHelper", BindingFlags.Static | BindingFlags.NonPublic); // reflection call to the generic get-method generator to generate the type arguments MethodInfo constructedHelper = genericHelper.MakeGenericMethod( method.DeclaringType, method.ReturnType); // now call it. The null argument is because it's a static method. object ret = constructedHelper.Invoke(null, new object[] { method }); // cast the result to the action delegate and return it return (Func<object, object>) ret; } static Func<object, object> GetGetterHelper<TTarget, TResult>(MethodInfo method) where TTarget : class // target must be a class as property sets on structs need a ref param { // Convert the slow MethodInfo into a fast, strongly typed, open delegate Func<TTarget, TResult> func = (Func<TTarget, TResult>) Delegate.CreateDelegate(typeof(Func<TTarget, TResult>), method); // Now create a more weakly typed delegate which will call the strongly typed one Func<object, object> ret = (object target) => (TResult) func((TTarget) target); return ret; }}
public static DataTable ToDataTable<T>(this IEnumerable<T> items) where T: class{ // ... create table the same way var propGetters = new List<Func<T, object>>();foreach (var prop in props) { Func<T, object> func = (Func<T, object>) ReflectionUtility.GetGetter(prop); propGetters.Add(func); } // Add the property values per T as rows to the datatable foreach (var item in items) { var values = new object[props.Length]; for (var i = 0; i < props.Length; i++) { //values[i] = props[i].GetValue(item, null); values[i] = propGetters[i](item); } table.Rows.Add(values); } return table; }
- 3 回答
- 0 關(guān)注
- 688 瀏覽
添加回答
舉報
0/150
提交
取消