• C# 反射获取对象的内容


    private static string GetTypeInfo(object obj)
            {
                Type t = obj.GetType();
                StringBuilder sb = new StringBuilder();
                foreach (PropertyInfo pi in t.GetProperties())
                {
                    object value = pi.GetValue(obj, null);//用pi.GetValue获得值
                    string name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                                          //获得属性的类型,进行判断然后进行以后的操作,例如判断获得的属性是整数
                    if (value.GetType() == typeof(int))
                    {
                        //进行你想要的操作
                    }
                    sb.Append(name + "" + value.ToString() + ":" + value.GetType().ToString() + ";");
                }
                return sb.ToString();
            }

    基础代码记录

  • 相关阅读:
    表达式for loop
    用户输入
    字符编码
    变量字符编码
    Python安装
    Python 2 or 3?
    Python解释器
    2017中国大学生程序设计竞赛
    Educational Round 27
    Round #429 (Div.2)
  • 原文地址:https://www.cnblogs.com/ChenRihe/p/7150198.html
Copyright © 2020-2023  润新知