Response.Write(getProperties(l1.Quote)); //项目中的代码。这样也没有遍历所有的 属性的属性吧,,
public 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);
{
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;
}
{
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;
}
C#反射获取对象属性
本文介绍了一种使用C#反射来获取任意对象所有公共属性的方法,并通过递归方式处理对象属性中的子对象属性。
1220

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



