public static string SortParam<T>(T t)
{
string tStr = string.Empty;
if (t == null)
{
return string.Empty;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return string.Empty;
}
foreach (System.Reflection.PropertyInfo item in properties.OrderBy(m => m.Name))//排序后组成字符串
{
string name = item.Name;//字段名称
object value = item.GetValue(t, null);//值
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
if (value != null)
tStr += (string.Format("{0}{1}", name, value));
}
else
{
SortParam(value);//集合字段递归
}
}
//tStr = tStr.OrderBy(m => m).ToList();
return tStr;
}
public IList<T> GetData(SqlDataReader reader)
{
IList<T> list = new List<T>();
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
while (reader.Read())
{
T t = Activator.CreateInstance<T>();
for (int i = 0; i < properties.Length; i++)
{
properties[i].SetValue(t, reader[i + 1], null);
}
list.Add(t);
}
return list;
}