示例#1
0
        /// <summary>
        /// 根据XML信息填充实体
        /// </summary>
        /// <typeparam name="T">MessageBase为基类的类型 http://stackoverflow.com/questions/4737970/what-does-where-t-class-new-mean </typeparam>
        /// <param name="entity">实体</param>
        /// <param name="xml">XML</param>
        public static void FillWithXML <T>(this T entity, XDocument doc) where T : class, new()
        {
            entity = entity ?? new T();
            var root  = doc.Root;
            var props = entity.GetType().GetProperties();

            foreach (var prop in props)
            {
                var propName = prop.Name;
                if (root.Element(propName) != null)
                {
                    switch (prop.PropertyType.Name)
                    {
                    case "DateTime":
                        prop.SetValue(entity, DateTimeHelper.ConvertToDateTime(root.Element(propName).Value), null);
                        break;

                    case "Boolean":
                        prop.SetValue(entity, root.Element(propName).Value == "1", null);
                        break;

                    case "Int32":
                        prop.SetValue(entity, int.Parse(root.Element(propName).Value), null);
                        break;

                    case "Int64":
                        prop.SetValue(entity, long.Parse(root.Element(propName).Value), null);
                        break;

                    case "Double":
                        prop.SetValue(entity, double.Parse(root.Element(propName).Value), null);
                        break;

                    case "RequestMsgType":
                        //已设为只读
                        //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null);
                        break;

                    case "ResponseMsgType":    //Response适用
                        //已设为只读
                        //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null);
                        break;

                    case "Event":
                        //已设为只读
                        //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null);
                        break;

                    case "EventType":
                        //已设为只读
                        break;

                    case "List`1":                                 //List<T>类型,ResponseMessageNews适用
                        var genericArguments = prop.PropertyType.GetGenericArguments();
                        if (genericArguments[0].Name == "Article") //ResponseMessageNews适用
                        {
                            //文章下属节点item
                            List <Article> articles = new List <Article>();
                            foreach (var item in root.Element(propName).Elements("item"))
                            {
                                var article = new Article();
                                FillWithXML(article, new XDocument(item));
                                articles.Add(article);
                            }
                            prop.SetValue(entity, articles, null);
                        }
                        //else if (genericArguments[0].Name == "Account")
                        //{
                        //    List<CustomerServiceAccount> accounts = new List<CustomerServiceAccount>();
                        //    foreach (var item in root.Elements(propName))
                        //    {
                        //        var account = new CustomerServiceAccount();
                        //        FillWithXML(account, new XDocument(item));
                        //        accounts.Add(account);
                        //    }
                        //    prop.SetValue(entity, accounts, null);
                        //}
                        else if (genericArguments[0].Name == "PicItem")
                        {
                            List <PicItem> picItems = new List <PicItem>();
                            foreach (var item in root.Elements(propName).Elements("item"))
                            {
                                var    picItem   = new PicItem();
                                var    picMd5Sum = item.Element("PicMd5Sum").Value;
                                Md5Sum md5Sum    = new Md5Sum()
                                {
                                    PicMd5Sum = picMd5Sum
                                };
                                picItem.item = md5Sum;
                                picItems.Add(picItem);
                            }
                            prop.SetValue(entity, picItems, null);
                        }
                        //else if (genericArguments[0].Name == "AroundBeacon")
                        //{
                        //    List<AroundBeacon> aroundBeacons = new List<AroundBeacon>();
                        //    foreach (var item in root.Elements(propName).Elements("AroundBeacon"))
                        //    {
                        //        var aroundBeaconItem = new AroundBeacon();
                        //        FillWithXML(aroundBeaconItem, new XDocument(item));
                        //        aroundBeacons.Add(aroundBeaconItem);
                        //    }
                        //    prop.SetValue(entity, aroundBeacons, null);
                        //}
                        break;

                    case "Music":    //ResponseMessageMusic适用
                        Music music = new Music();
                        FillWithXML(music, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, music, null);
                        break;

                    case "Image":    //ResponseMessageImage适用
                        Image image = new Image();
                        FillWithXML(image, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, image, null);
                        break;

                    case "Voice":    //ResponseMessageVoice适用
                        Voice voice = new Voice();
                        FillWithXML(voice, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, voice, null);
                        break;

                    case "Video":    //ResponseMessageVideo适用
                        Video video = new Video();
                        FillWithXML(video, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, video, null);
                        break;

                    case "ScanCodeInfo":    //扫码事件中的ScanCodeInfo适用
                        ScanCodeInfo scanCodeInfo = new ScanCodeInfo();
                        FillWithXML(scanCodeInfo, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, scanCodeInfo, null);
                        break;

                    case "SendLocationInfo":    //弹出地理位置选择器的事件推送中的SendLocationInfo适用
                        SendLocationInfo sendLocationInfo = new SendLocationInfo();
                        FillWithXML(sendLocationInfo, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, sendLocationInfo, null);
                        break;

                    case "SendPicsInfo":    //系统拍照发图中的SendPicsInfo适用
                        SendPicsInfo sendPicsInfo = new SendPicsInfo();
                        FillWithXML(sendPicsInfo, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, sendPicsInfo, null);
                        break;

                    //case "ChosenBeacon"://摇一摇事件通知
                    //    ChosenBeacon chosenBeacon = new ChosenBeacon();
                    //    FillWithXML(chosenBeacon, new XDocument(root.Element(propName)));
                    //    prop.SetValue(entity, chosenBeacon, null);
                    //    break;
                    //case "AroundBeacon"://摇一摇事件通知
                    //    AroundBeacon aroundBeacon = new AroundBeacon();
                    //    FillWithXML(aroundBeacon, new XDocument(root.Element(propName)));
                    //    prop.SetValue(entity, aroundBeacon, null);
                    //    break;
                    default:
                        prop.SetValue(entity, root.Element(propName).Value, null);
                        break;
                    }
                }
            }
        }
 public RequestMessageEventLocationSelect()
 {
     SendLocationInfo = new SendLocationInfo();
 }