---
title: "遍历类的属性"
author: "liuway"
date: "2009-08-12T11:23:10Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-08-12T11:23:10Z"
id: 445398
url: "https://liuway.iteye.com/blog/445398"
---

Response.Write(getProperties(l1.Quote)); //项目中的代码。这样也没有遍历所有的 属性的属性吧，，

 public string getProperties&lt;T&gt;(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 &lt;= 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;  
    }