zoukankan      html  css  js  c++  java
  • C#通过反射获取相应的字段和值

    代码比较简单,只作为简单的例子参考

    首先先看运行的代码:

     class Program
        {
            static void Main(string[] args)
            {
                UserInfo userInfo = new UserInfo();
                userInfo.ID = 1;
                userInfo.Name = "bailey";
                userInfo.CreateDate = DateTime.Now;
                userInfo.Number = Convert.ToDecimal(456.6467);
    
                string values = string.Empty;
                foreach (System.Reflection.PropertyInfo p in userInfo.GetType().GetProperties())
                {
                    if (p.PropertyType == typeof(string))
                    {
                        values += string.Format("{0}='{1}', ", p.Name, p.GetValue(userInfo));
                    }
                    if (p.PropertyType == typeof(int)|| p.PropertyType == typeof(uint))
                    {
                        values += string.Format("{0}={1},", p.Name, p.GetValue(userInfo));
                    }
                    if (p.PropertyType == typeof(DateTime))
                    {
                        values += string.Format("{0}='{1}', ", p.Name, p.GetValue(userInfo));
                    }
                    if (p.PropertyType == typeof(decimal) || p.PropertyType == typeof(double)|| p.PropertyType == typeof(float))
                    {
                        values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                    }
                   
                    if (p.PropertyType == typeof(bool))
                    {
                        values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                    }
                    if (p.PropertyType == typeof(sbyte))
                    {
                        values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                    }
                    if (p.PropertyType == typeof(byte) || p.PropertyType == typeof(short) || p.PropertyType == typeof(ushort) )
                    {
                        values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                    }
                    if (p.PropertyType == typeof(long) || p.PropertyType == typeof(ulong))
                    {
                        values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo));
                    }
    
                    // values +=string.Format( "{0}={1},", p.Name, p.GetValue(userInfo));
                    // Console.WriteLine("Name:{0} Value:{1}", p.Name, p.GetValue(userInfo));
                }
                Console.WriteLine(values);
                Console.ReadLine();
            }
    
        }

    再看对象:

     class UserInfo {
            public int ID { get; set; }
            public string Name { get; set; }
            public DateTime? CreateDate { get; set; }
            public decimal? Number { get; set; }
            public bool IsUse { get; set; }
        }
  • 相关阅读:
    XML文件详解以及解析
    Delphi 泛型详解
    Delphi 修改本地日期和时间
    java -> this关键字
    java ->super关键字
    Java -> 构造器(构造方法)
    java -> 方法的重载
    java面向对象->多态
    Java面向对象->接口
    Java面向对象->抽象类
  • 原文地址:https://www.cnblogs.com/bmyblogs/p/9346843.html
Copyright © 2011-2022 走看看