/// <summary>
/// 获取传入对象的属性值,格式为(属性:值),
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns>属性与值拼接成的字符串,如(属性:值),(属性:值),(属性:值),</returns>
public static string GetProperties<T>(T t)
{
string tStr = string.Empty;
if (t == null)
{
return tStr;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return tStr;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
tStr += string.Format("{0}:{1},", name, value);
}
else
{
GetProperties(value);
}
}
return tStr;
}
/// 获取传入对象的属性值,格式为(属性:值),
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns>属性与值拼接成的字符串,如(属性:值),(属性:值),(属性:值),</returns>
public static string GetProperties<T>(T t)
{
string tStr = string.Empty;
if (t == null)
{
return tStr;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return tStr;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
tStr += string.Format("{0}:{1},", name, value);
}
else
{
GetProperties(value);
}
}
return tStr;
}