第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將IEnDigable轉(zhuǎn)換為DataTable

將IEnDigable轉(zhuǎn)換為DataTable

滄海一幻覺 2019-07-13 14:49:19
是否有一種很好的方法將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關(guān)于值類型,除非它們是經(jīng)過的ref.

第二GetValue很慢而且經(jīng)常被打電話。

為了解決這個問題,我們可以創(chuàng)建一個委托,然后調(diào)用它:

MethodInfo method = property.GetGetMethod(true);Delegate.CreateDelegate(typeof(Func<TClass, TProperty>), method );

問題是我們不知道TProperty,但像往常一樣喬恩·斯基特有答案-我們可以使用反射檢索getter委托,但是一旦有了它,我們就不需要再進(jìn)行反射:

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;
    }}

現(xiàn)在,您的方法變成:

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; }

您可以通過在靜態(tài)字典中存儲每個類型的getter來進(jìn)一步優(yōu)化它,然后對每個類型只進(jìn)行一次反射開銷。


查看完整回答
反對 回復(fù) 2019-07-13
  • 3 回答
  • 0 關(guān)注
  • 688 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號