反射实际运用

来源:互联网 发布:matlab矩阵的取值 编辑:程序博客网 时间:2024/06/06 08:39
应用背景:做前端开发时,需要解析从后端传过来的数据,放到实体类里面。现在后端传过来一个商品列表的xml,子节点有a,b,c三个属性,分别对应Code,Name和Number现在新建一个ShopCode类,有Code,Name, Number三个属性,一般的做法是新建一个ShopCode类,然后一一对应赋值,然后把ShopCode这个类放到ShopCode的集合里面,但是通过反射可以在不知道给某个类有多少属性的情况下赋值,这样子在数据接收的时候,可以用同一个方法解决数据的存放问题.代码如下

属性类 (继承 Attribute)
namespace RefectionDemo
{
    public class StorgAttribute:Attribute
    {
        public string name;

        public StorgAttribute(string  _name)
        {
            this.name = _name;
        }

    }
}

实体类的类型(枚举类型)
namespace RefectionDemo
{
    public enum ModelType
    {
        None = 0,
        ShopGood = 1,
        MessageItem = 2,
    }
}



反射基类
namespace RefectionDemo
{
    public class ReflectionBase
    {
        public ReflectionBase(XmlNode _listNode, ModelType _modelType)
        {
            this.Initialize(_listNode, _modelType);
        }

        public ReflectionBase()
        {
        
        }

        private void Initialize(XmlNode _listNode, ModelType _modelType)
        {
            Type _type = this.GetType();
            PropertyInfo[] _properties = _type.GetProperties();
            XmlElement _nodeElement;
            StorgAttribute _itemAttrubite;
            string _itemAttributeName;
            ReflectionBase _base = null;

            foreach(XmlNode _item in _listNode)
            {
                _nodeElement = (XmlElement)_item;
                ReflectionBase _reflection = new ReflectionBase();

                switch (_modelType)
                {
                    case ModelType.ShopGood:
                        {
                            _base = new ShopGood();

                            break;
                        }
                    case ModelType.MessageItem:
                        {
                            _base = new MessageItem();

                            break;
                        }
                }

                foreach(PropertyInfo _property in _properties)
                {
                    _itemAttrubite = (StorgAttribute)_property.GetCustomAttribute(typeof(StorgAttribute));
                    _itemAttributeName = _itemAttrubite.name;

                    if (_property.CanWrite && _nodeElement.HasAttribute(_itemAttributeName))
                    {
                        _property.SetValue(_base, Convert.ChangeType(_item.Attributes[_itemAttributeName].Value, _property.PropertyType), null);
                    }
                }

                switch(_modelType)
                {
                    case ModelType.ShopGood:
                        {
                Global.ShopGoodList.SetItem((ShopGood)_base);

                            break;
                        }
                    case ModelType.MessageItem:
                        {
                            Global.MessageConnection.SetItem((MessageItem)_base);

                            break;
                        }
                }
            }

        }
    }
}


实体类(商品) 继承反射基类
namespace RefectionDemo
{
    public class MessageItem:ReflectionBase
    {
         public MessageItem(XmlNode _xmlNode, ModelType _modelType)
            : base(_xmlNode, _modelType)
        {

        }

         public MessageItem()
        {
        
        }
        

        [StorgAttribute("a")]
        public string Code
        {
            get;
            set;
        }

        [StorgAttribute("b")]
        public string Content
        {
            get;
            set;
        }

    }
}


实体类的集合
namespace RefectionDemo
{
    public class ShopGoodCollection :List<ShopGood>
    {
        public void SetItem(ShopGood t)
        {
            if (this.Contains(t))
            {
                ShopGood _item = null;
                for (int i = 0; i < this.Count; i++)
                {
                    _item = this[i];

                    if (_item.Code == t.Code)
                    {
                        _item.Name = t.Name;
                        _item.Number = t.Number;
                    }
                }
            }
            else
            {
                this.Add(t);
            }
        }

    }
}

namespace RefectionDemo
{
    public class MessageCollection:List<MessageItem>
    {
        public void SetItem(MessageItem messageItem)
        {
            if (this.IsContain(messageItem.Code))
            {
                MessageItem _item = null;
                for (int i = 0; i < this.Count; i++)
                {
                    _item = this[i];

                    if (_item.Code == messageItem.Code)
                    {
                        _item.Content = messageItem.Content;
                    }
                }
            }
            else
            {
                this.Add(messageItem);
            }
        }

        public bool IsContain(string _code)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if(this[i].Code == _code)
                {
                    return true;
                }
            }

            return false;
        }


    }
}





全局类 存放实体类的列表
namespace RefectionDemo
{
    public class Global
    {
        public static ShopGoodCollection ShopGoodList = new ShopGoodCollection();
        public static MessageCollection MessageConnection = new MessageCollection();



    }
}

控制台输出
namespace RefectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "<value>" +
               "<R a='1000,0026' b='1天广告道具' c='10' />" +
               "<R a='1000,0027' b='2天广告道具' c='20' />" +
               "<R a='1000,0028' b='3天广告道具' c='30' />" +
               "<R a='1000,0029' b='4天广告道具' c='40' />" +
               "<R a='1000,0030' b='5天广告道具' c='50' />" +
               "<R a='1000,0031' b='6天广告道具' c='60' />" +
               "</value>";
            XmlDocument _list = new XmlDocument();
            _list.LoadXml(a);
            XmlNode _listNode = _list.ChildNodes[0];
            ModelType _type = ModelType.ShopGood;


            ShopGood _good = new ShopGood(_listNode, _type);

            foreach (ShopGood _shopGood in Global.ShopGoodList)
            {
                Console.WriteLine("{0} {1} {2}", _shopGood.Code, _shopGood.Name, _shopGood.Number);
            }


             a = "<value>" +
   "<R a='001' b='100000' />" +
   "<R a='002' b='200000' />" +
   "<R a='003' b='300000' />" +
   "<R a='004' b='400000' />" +
   "<R a='005' b='500000' />" +
   "</value>";
             _list = new XmlDocument();
            _list.LoadXml(a);
            _listNode = _list.ChildNodes[0];
            _type = ModelType.MessageItem;

            MessageItem _item = new MessageItem(_listNode, _type);


            foreach (MessageItem _messageItem in Global.MessageConnection)
            {
                Console.WriteLine("{0} {1}", _messageItem.Code, _messageItem.Content);
            }

            a = "<value>" +
"<R a='001' b='1' />" +
"<R a='002' b='2' />" +
"<R a='003' b='3' />" +
"<R a='004' b='4' />" +
"<R a='005' b='5' />" +
"</value>";
            _list = new XmlDocument();
            _list.LoadXml(a);
            _listNode = _list.ChildNodes[0];
            _type = ModelType.MessageItem;

            MessageItem _item1 = new MessageItem(_listNode, _type);


            foreach (MessageItem _messageItem in Global.MessageConnection)
            {
                Console.WriteLine("{0} {1}", _messageItem.Code, _messageItem.Content);
            }

        }
    }
}

OutPut
1000,0026 1天广告道具 1
1000,0027 2天广告道具 2
1000,0028 3天广告道具 3
1000,0029 4天广告道具 4
1000,0030 5天广告道具 5
1000,0031 6天广告道具 6
001 100000
002 200000
003 300000
004 400000
005 500000
001 1
002 2
003 3
004 4
005 5
请按任意键继续. . .














原创粉丝点击