泛型運用 走訪參數 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;
}
留言
張貼留言