泛型運用 走訪參數 string 轉換

 protected T GetParam<T>(T t , bool nullToEmpty = false) {


            var sParam = t.GetType().GetProperties()

                .Where(p => p.PropertyType == typeof(string) && p.CanWrite);


            foreach (var param in sParam) {

                string currentValue = (string)param.GetValue(t, null);

                if (currentValue != null)

                    param.SetValue(t, currentValue.Trim(), null);

                else if (currentValue == null && nullToEmpty == true)

                {

                    param.SetValue(t, "", null);

                }

            }


            return t;

        }

留言

這個網誌中的熱門文章

無法載入檔案或組件 'System.IO.Compression' 或其相依性的其中之一。

MongoDB 入門

javascript 更改屬性及創建標籤