c#的反射浅谈

来源:互联网 发布:张弘范 知乎 编辑:程序博客网 时间:2024/05/22 15:34

请看下面的代码,我试图解析一个对象,并获取里面的值

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Inventory i = new Inventory();
            Device d = new Device();
            DeviceClient dc = new DeviceClient();
            i.department = "fasd";
            i.holderName = "123";
            i.inventoryNo = "2345";
            i.location = "dddddd";
            d.deviceNo = "30002";
            d.inventory = i;
            d.type = "5555";
            dc.clientNo = "xh";
            dc.clientVersion = "1.0.1.18";
            dc.clientNo = "30070078";
            dc.device = d;
            dc.online = "true";
            dc.status = "1";

            const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
                BindingFlags.InvokeMethod | BindingFlags.GetProperty | BindingFlags.GetField;
            const BindingFlags flags2 = BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty;
            var tempT = dc.GetType().GetProperty("deviceNo");
            if (tempT == null)
            {
                var pies = dc.GetType().GetProperties();
                foreach (PropertyInfo p in pies)
                {
                    if (p.PropertyType != typeof(string))
                    {
                        object temp = p.GetValue(dc, null);

                        var target = temp.GetType().InvokeMember("deviceNo", flags, null, temp, null);
                        Console.WriteLine(target);
                    }
                }
            }

        }
    }
     public class DeviceClient
    {
        public string clientNo { get; set; }
        /// <summary>
        /// 是否已激活,1=激活;2=未激活。
        /// </summary>
        public string status { get; set; }
        /// <summary>
        /// Agent版本。
        /// </summary>
        public string clientVersion { get; set; }
        //public string expressOnline { get; set; }
        /// <summary>
        /// patrol状态。
        /// </summary>
        public string online { get; set; }

        private Device _device = new Device();
        public Device device
        {
            get{return _device;}
            set{_device = value;}
        }
    }
    /// <summary>
    /// 设备实体
    /// </summary>
    public class Device
    {
        private string _deviceNo;
        /// <summary>
        /// 设备编号。
        /// </summary>
        public string deviceNo
        {
            get
            {
                return _deviceNo;
            }
            set
            {
                _deviceNo = value;
            }
        }
        ///// <summary>
        ///// 设备唯一码。
        ///// </summary>
        //public string uniqueCode { get; set; }
        /// <summary>
        /// 设备类型。
        /// </summary>
        public string type { get; set; }
        ///// <summary>
        ///// 是否已激活,1=激活;2=未激活。
        ///// </summary>
        //public string status { get; set; }
        ///// <summary>
        ///// Agent版本。
        ///// </summary>
        //public string clientVersion { get; set; }
        ///// <summary>
        ///// Agent状态。
        ///// </summary>
        //public string expressOnline { get; set; }
    

        /// <summary>
        /// 资产
        /// </summary>
        private Inventory _inventory = new Inventory();
        public Inventory inventory
        {
            get { return _inventory; }
            set { _inventory = value; }
        }
    }

    /// <summary>
    /// 资产实体。
    /// </summary>
    public class Inventory
    {
        /// <summary>
        /// 资产编号。
        /// </summary>
        public string inventoryNo { get; set; }
        /// <summary>
        /// 所在地。
        /// </summary>
        public string location { get; set; }
        /// <summary>
        /// 部门名称。
        /// </summary>
        public string department { get; set; }

        /// <summary>
        /// 使用者。
        /// </summary>
        public string holderName { get; set; }
    }

}
我这里要说明的情况是这样的,请看下面的代码
static void Main(string[] args)
        {
            Inventory i = new Inventory();
            Device d = new Device();
            DeviceClient dc = new DeviceClient();
            i.department = "fasd";
            i.holderName = "123";
            i.inventoryNo = "2345";
            i.location = "dddddd";
            d.deviceNo = "30002";
            d.inventory = i;
            d.type = "5555";
            dc.clientNo = "xh";
            dc.clientVersion = "1.0.1.18";
            dc.clientNo = "30070078";
            dc.device = d;
            dc.online = "true";
            dc.status = "1";

            const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
                BindingFlags.InvokeMethod | BindingFlags.GetProperty | BindingFlags.GetField;
            const BindingFlags flags2 = BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty;
            var tempT = dc.GetType().GetProperty("deviceNo");
            if (tempT == null)
            {
                var pies = dc.GetType().GetProperties();
                foreach (PropertyInfo p in pies)
                {
                    if (p.PropertyType != typeof(string))
                    {
                         var pEntries = p.GetType().GetProperties();

                         foreach(var pe in pEntries)

                        {

                             //do everything you want

                          }

                    }
                }
            }

        }
不能将PropertyInfo 在继续获取属性,因为他的Type已经是PropertyInfo,所以很容易混淆,如果序列化一个对象,我们一般采用xmlserilize或者binaryserilize对象来处理,,但是如果是多对象,比如我上面提到的,其实也可以采用反序列化xmldeserialize或者2进制反序列化,也可以采用利用这种方式,这个我们以后讨论