反射

 public static StringBuilder GetProperties(object obj)
        {
            if (obj == null) return null;

            Type t = obj.GetType();
            PropertyInfo[] propertyInfo = t.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

            StringBuilder sb = new StringBuilder();
            foreach (PropertyInfo pInof in propertyInfo)
            {
                string strLine = pInof.Name + " = ";
                object objValue = pInof.GetValue(obj, null);

                if (objValue != null && pInof.PropertyType.IsClass && !(objValue is String))
                {
                    GetProperties(objValue);
                }
                else
                {
                    if (objValue != null)
                    {
                        strLine += objValue.ToString();

                    }
                }

                sb.AppendLine(strLine);
            }//foreach

            return sb;

        }

 

你可能感兴趣的:(反射)