`
liuway
  • 浏览: 57101 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

遍历类的属性

    博客分类:
  • .net
阅读更多
 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);
        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;
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics