控制台程序:
class Program
{
static void Main(string[] args)
{
User u = new User();
u.name = "ahbool";
u.gender = "男";
u.age = "1";
Console.WriteLine(GetProperties(u));
}
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;
}
}
public class User
{
public string name { get; set; }
public string gender { get; set; }
public string age { get; set; }
}输出效果:
name:ahbool,gender:男,age:1,

这篇博客探讨了如何将一个包含个人信息的对象转换成键值对的格式,例如name:ahbool, gender:男, age:1,适用于数据解析或存储。"
105770052,9351719,Java数组异常处理详解,"['Java', '异常处理', '数组操作']
2614

被折叠的 条评论
为什么被折叠?



