至高吴上(Alfa.wu)

一个人,一生,能坚持做好一件事情是多么的牛XX啊!!!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
public string getNameAndValue<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
                {
                    getNameAndValue(value);
                }
            }
            return tStr;
        }

 

posted on 2012-09-27 20:39  Alfa  阅读(4366)  评论(0编辑  收藏  举报