コード例 #1
0
        public void FillSystemTypeTest()
        {
            var obj  = new TestCustomObject();
            var prop = typeof(TestCustomObject).GetProperties().FirstOrDefault(z => z.Name == "Id");

            EntityUtility.FillSystemType(obj, prop, "666");
            Assert.AreEqual(666, obj.Id);

            obj.Markers = 100;
            prop        = typeof(TestCustomObject).GetProperties().FirstOrDefault(z => z.Name == "Markers");
            EntityUtility.FillSystemType(obj, prop, "");
            Assert.AreEqual(null, obj.Markers);


            obj.ArrInt = new[] { 987, 543 };
            prop       = typeof(TestCustomObject).GetProperties().FirstOrDefault(z => z.Name == "ArrInt");
            EntityUtility.FillSystemType(obj, prop, "123,456,789");
            Assert.AreEqual(3, obj.ArrInt.Length);
            Assert.AreEqual(123, obj.ArrInt[0]);
            Assert.AreEqual(456, obj.ArrInt[1]);
            Assert.AreEqual(789, obj.ArrInt[2]);


            obj.ArrLong = new[] { (long)987, (long)543 };
            prop        = typeof(TestCustomObject).GetProperties().FirstOrDefault(z => z.Name == "ArrLong");
            EntityUtility.FillSystemType(obj, prop, "123,456,789");
            Assert.AreEqual(3, obj.ArrLong.Length);
            Assert.AreEqual(123, obj.ArrLong[0]);
            Assert.AreEqual(456, obj.ArrLong[1]);
            Assert.AreEqual(789, obj.ArrLong[2]);
        }
コード例 #2
0
        public static void FillEntityWithXml <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 "String":
                    //    goto default;
                    case "DateTime":
                    case "Int32":
                    case "Int64":
                    case "Double":
                    case "Nullable`1":     //可为空对象
                        EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value);
                        break;

                    case "Boolean":
                        if (propName == "FuncFlag")
                        {
                            EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1");
                        }
                        else
                        {
                            goto default;
                        }
                        break;

                    //以下为枚举类型
                    case "RequestInfoType":
                        //已设为只读
                        break;

                    default:
                        prop.SetValue(entity, root.Element(propName).Value, null);
                        break;
                    }
                }
            }
        }
コード例 #3
0
ファイル: EntityHelper.cs プロジェクト: wyh0395/Berry
        /// <summary>
        /// 根据XML信息填充实实体
        /// </summary>
        /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam>
        /// <param name="entity">实体</param>
        /// <param name="doc">XML</param>
        public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new()
        {
            entity = entity ?? new T();
            var root = doc.Root;

            if (root == null)
            {
                return;//无法处理
            }

            var props = entity.GetType().GetProperties();

            foreach (var prop in props)
            {
                var propName = prop.Name;
                if (root.Element(propName) != null)
                {
                    switch (prop.PropertyType.Name)
                    {
                    //case "String":
                    //    goto default;
                    case "DateTime":
                    case "Int32":
                    case "Int64":
                    case "Double":
                    case "Nullable`1":     //可为空对象
                        EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value);
                        break;

                    case "Boolean":
                        if (propName == "FuncFlag")
                        {
                            EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1");
                        }
                        else
                        {
                            goto default;
                        }
                        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 "List`1":     //List<T>类型,ResponseMessageNews适用
                    {
                        var genericArguments        = prop.PropertyType.GetGenericArguments();
                        var genericArgumentTypeName = genericArguments[0].Name;
                        if (genericArgumentTypeName == "Article")
                        {
                            //文章下属节点item
                            List <Article> articles = new List <Article>();
                            foreach (var item in root.Element(propName).Elements("item"))
                            {
                                var article = new Article();
                                FillEntityWithXml(article, new XDocument(item));
                                articles.Add(article);
                            }
                            prop.SetValue(entity, articles, null);
                        }
                        else if (genericArgumentTypeName == "Account")
                        {
                            List <CustomerServiceAccount> accounts = new List <CustomerServiceAccount>();
                            foreach (var item in root.Elements(propName))
                            {
                                var account = new CustomerServiceAccount();
                                FillEntityWithXml(account, new XDocument(item));
                                accounts.Add(account);
                            }
                            prop.SetValue(entity, accounts, null);
                        }
                        else if (genericArgumentTypeName == "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 (genericArgumentTypeName == "AroundBeacon")
                        {
                            List <AroundBeacon> aroundBeacons = new List <AroundBeacon>();
                            foreach (var item in root.Elements(propName).Elements("AroundBeacon"))
                            {
                                var aroundBeaconItem = new AroundBeacon();
                                FillEntityWithXml(aroundBeaconItem, new XDocument(item));
                                aroundBeacons.Add(aroundBeaconItem);
                            }
                            prop.SetValue(entity, aroundBeacons, null);
                        }
                        else if (genericArgumentTypeName == "CopyrightCheckResult_ResultList")        //RequestMessageEvent_MassSendJobFinish
                        {
                            List <CopyrightCheckResult_ResultList> resultList = new List <CopyrightCheckResult_ResultList>();
                            foreach (var item in root.Elements("ResultList").Elements("item"))
                            {
                                CopyrightCheckResult_ResultList resultItem = new CopyrightCheckResult_ResultList();
                                FillEntityWithXml(resultItem.item, new XDocument(item));
                                resultList.Add(resultItem);
                            }
                            prop.SetValue(entity, resultList, null);
                        }
                        break;
                    }

                    case "Music":    //ResponseMessageMusic适用
                        FillClassValue <Music>(entity, root, propName, prop);
                        break;

                    case "Image":    //ResponseMessageImage适用
                        FillClassValue <Image>(entity, root, propName, prop);
                        break;

                    case "Voice":    //ResponseMessageVoice适用
                        FillClassValue <Voice>(entity, root, propName, prop);
                        break;

                    case "Video":    //ResponseMessageVideo适用
                        FillClassValue <Video>(entity, root, propName, prop);
                        break;

                    case "ScanCodeInfo":    //扫码事件中的ScanCodeInfo适用
                        FillClassValue <ScanCodeInfo>(entity, root, propName, prop);
                        break;

                    case "SendLocationInfo":    //弹出地理位置选择器的事件推送中的SendLocationInfo适用
                        FillClassValue <SendLocationInfo>(entity, root, propName, prop);
                        break;

                    case "SendPicsInfo":    //系统拍照发图中的SendPicsInfo适用
                        FillClassValue <SendPicsInfo>(entity, root, propName, prop);
                        break;

                    case "ChosenBeacon":    //摇一摇事件通知
                        FillClassValue <ChosenBeacon>(entity, root, propName, prop);
                        break;

                    case "AroundBeacon":    //摇一摇事件通知
                        FillClassValue <AroundBeacon>(entity, root, propName, prop);
                        break;

                        #region RequestMessageEvent_MassSendJobFinish
                    case "CopyrightCheckResult":
                        FillClassValue <CopyrightCheckResult>(entity, root, propName, prop);
                        break;

                    case "CopyrightCheckResult_ResultList_Item":
                        FillClassValue <CopyrightCheckResult_ResultList_Item>(entity, root, "item", prop);
                        break;
                        #endregion

                    default:
                        var enumSuccess = false;
                        if (prop.PropertyType.IsEnum)
                        {
                            //未知的枚举类型
                            try
                            {
                                prop.SetValue(entity, Enum.Parse(prop.PropertyType, root.Element(propName).Value, true), null);
                                enumSuccess = true;
                            }
                            catch {
                            }
                        }

                        if (!enumSuccess)
                        {
                            prop.SetValue(entity, root.Element(propName).Value, null);
                        }
                        break;
                    }
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// 根据XML信息填充实实体
        /// </summary>
        /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam>
        /// <param name="entity">实体</param>
        /// <param name="doc">XML</param>
        public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ 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 "String":
                    //    goto default;
                    case "DateTime":
                    case "DateTimeOffset":
                    case "Int32":
                    case "Int32[]":     //增加int[]
                    case "Int64":
                    case "Int64[]":     //增加long[]
                    case "Double":
                    case "Nullable`1":  //可为空对象
                        EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value);
                        break;

                    case "Boolean":
                        if (propName == "FuncFlag")
                        {
                            EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1");
                        }
                        else
                        {
                            goto default;
                        }
                        break;

                    //以下为枚举类型
                    case "ContactChangeType":
                        //已设为只读
                        //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(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 "ThirdPartyInfo":     //ThirdPartyInfo适用
                                               //已设为只读
                                               //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 "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();
                                FillEntityWithXml(article, new XDocument(item));
                                articles.Add(article);
                            }
                            prop.SetValue(entity, articles, null);
                        }
                        else if (genericArguments[0].Name == "MpNewsArticle")
                        {
                            List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>();
                            foreach (var item in root.Elements(propName))
                            {
                                var mpNewsArticle = new MpNewsArticle();
                                FillEntityWithXml(mpNewsArticle, new XDocument(item));
                                mpNewsArticles.Add(mpNewsArticle);
                            }
                            prop.SetValue(entity, mpNewsArticles, 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);
                        }
                        break;

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

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

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

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

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

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

                    case "BatchJobInfo":     //异步任务完成事件推送BatchJob
                        BatchJobInfo batchJobInfo = new BatchJobInfo();
                        FillEntityWithXml(batchJobInfo, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, batchJobInfo, null);
                        break;

                    case "AgentType":
                    {
                        AgentType tp;
#if NET35
                        try
                        {
                            tp = (AgentType)Enum.Parse(typeof(AgentType), root.Element(propName).Value, true);
                            prop.SetValue(entity, tp, null);
                        }
                        catch
                        {
                        }
#else
                        if (Enum.TryParse(root.Element(propName).Value, out tp))
                        {
                            prop.SetValue(entity, tp, null);
                        }
#endif
                        break;
                    }

                    case "Receiver":
                    {
                        Receiver receiver = new Receiver();
                        FillEntityWithXml(receiver, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, receiver, null);
                        break;
                    }

                    default:
                        prop.SetValue(entity, root.Element(propName).Value, null);
                        break;
                    }
                }
                else if (prop.PropertyType.Name == "List`1")//客服回调特殊处理
                {
                    var genericArguments = prop.PropertyType.GetGenericArguments();
                    if (genericArguments[0].Name == "RequestBase")
                    {
                        List <RequestBase> items = new List <RequestBase>();
                        foreach (var item in root.Elements("Item"))
                        {
                            RequestBase reqItem    = null;
                            var         msgTypeEle = item.Element("MsgType");
                            if (msgTypeEle != null)
                            {
                                RequestMsgType type         = RequestMsgType.Unknown;
                                var            parseSuccess = false;
#if NET35
                                try
                                {
                                    type         = (RequestMsgType)Enum.Parse(typeof(RequestMsgType), msgTypeEle.Value, true);
                                    parseSuccess = true;
                                }
                                catch
                                {
                                }
#else
                                parseSuccess = Enum.TryParse(msgTypeEle.Value, true, out type);
#endif
                                if (parseSuccess)
                                {
                                    switch (type)
                                    {
                                    case RequestMsgType.Event:
                                    {
                                        reqItem = new RequestEvent();
                                        break;
                                    }

                                    case RequestMsgType.File:
                                    {
                                        reqItem = new Entities.Request.KF.RequestMessageFile();
                                        break;
                                    }

                                    case RequestMsgType.Image:
                                    {
                                        reqItem = new Entities.Request.KF.RequestMessageImage();
                                        break;
                                    }

                                    case RequestMsgType.Link:
                                    {
                                        reqItem = new Entities.Request.KF.RequestMessageLink();
                                        break;
                                    }

                                    case RequestMsgType.Location:
                                    {
                                        reqItem = new Entities.Request.KF.RequestMessageLocation();
                                        break;
                                    }

                                    case RequestMsgType.Text:
                                    {
                                        reqItem = new Entities.Request.KF.RequestMessageText();

                                        break;
                                    }

                                    case RequestMsgType.Voice:
                                    {
                                        reqItem = new Entities.Request.KF.RequestMessageVoice();
                                        break;
                                    }
                                    }
                                }
                            }
                            if (reqItem != null)
                            {
                                FillEntityWithXml(reqItem, new XDocument(item));
                                items.Add(reqItem);
                            }
                        }
                        prop.SetValue(entity, items, null);
                    }
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// 根据XML信息填充实实体
        /// </summary>
        /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam>
        /// <param name="entity">实体</param>
        /// <param name="doc">XML</param>
        public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ 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 "String":
                    //    goto default;
                    case "DateTime":
                    case "Int32":
                    case "Int64":
                    case "Double":
                    case "Nullable`1":     //可为空对象
                        EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value);
                        break;

                    case "Boolean":
                        if (propName == "FuncFlag")
                        {
                            EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1");
                        }
                        else
                        {
                            goto default;
                        }
                        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 "ThirdPartyInfo":    //ThirdPartyInfo适用
                        //已设为只读
                        //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 "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();
                                FillEntityWithXml(article, new XDocument(item));
                                articles.Add(article);
                            }
                            prop.SetValue(entity, articles, null);
                        }
                        else if (genericArguments[0].Name == "MpNewsArticle")
                        {
                            List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>();
                            foreach (var item in root.Elements(propName))
                            {
                                var mpNewsArticle = new MpNewsArticle();
                                FillEntityWithXml(mpNewsArticle, new XDocument(item));
                                mpNewsArticles.Add(mpNewsArticle);
                            }
                            prop.SetValue(entity, mpNewsArticles, 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);
                        }
                        break;

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

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

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

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

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

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

                    case "BatchJobInfo":    //异步任务完成事件推送BatchJob
                        BatchJobInfo batchJobInfo = new BatchJobInfo();
                        FillEntityWithXml(batchJobInfo, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, batchJobInfo, null);
                        break;

                    default:
                        prop.SetValue(entity, root.Element(propName).Value, null);
                        break;
                    }
                }
            }
        }
コード例 #6
0
ファイル: EntityHelper.cs プロジェクト: vivisoft2019/NeuChar
        /// <summary>
        /// 根据XML信息填充实实体
        /// </summary>
        /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam>
        /// <param name="entity">实体</param>
        /// <param name="doc">XML</param>
        public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new()
        {
            if (doc == null)
            {
                throw new ArgumentNullException(nameof(doc));
            }

            var root = doc.Root;

            if (doc.Root == null)
            {
                throw new ArgumentNullException(nameof(doc.Root));
            }

            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            //entity = entity ?? new T();

            var props = entity.GetType().GetProperties();

            foreach (var prop in props)
            {
                if (!prop.CanWrite)
                {
                    continue;//如果不可读则跳过
                }

                var propName = prop.Name;
                if (root.Element(propName) != null)
                {
                    switch (prop.PropertyType.Name)
                    {
                    //case "String":
                    //    goto default;
                    case "DateTime":
                    case "DateTimeOffset":
                    case "Int32":
                    case "Int64":
                    case "Double":
                    case "Nullable`1":     //可为空对象
                        EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value);
                        break;

                    case "Boolean":
                        if (propName == "FuncFlag")
                        {
                            EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1");
                        }
                        else
                        {
                            goto default;
                        }
                        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 "List`1":     //List<T>类型,ResponseMessageNews适用
                    {
                        var genericArguments        = prop.PropertyType.GetGenericArguments();
                        var genericArgumentTypeName = genericArguments[0].Name;
                        if (genericArgumentTypeName == "Article")
                        {
                            //文章下属节点item
                            List <Article> articles = new List <Article>();
                            foreach (var item in root.Element(propName).Elements("item"))
                            {
                                var article = new Article();
                                FillEntityWithXml(article, new XDocument(item));
                                articles.Add(article);
                            }
                            prop.SetValue(entity, articles, null);
                        }
                        else if (genericArgumentTypeName == "Account")
                        {
                            List <CustomerServiceAccount> accounts = new List <CustomerServiceAccount>();
                            foreach (var item in root.Elements(propName))
                            {
                                var account = new CustomerServiceAccount();
                                FillEntityWithXml(account, new XDocument(item));
                                accounts.Add(account);
                            }
                            prop.SetValue(entity, accounts, null);
                        }
                        else if (genericArgumentTypeName == "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 (genericArgumentTypeName == "AroundBeacon")
                        {
                            List <AroundBeacon> aroundBeacons = new List <AroundBeacon>();
                            foreach (var item in root.Elements(propName).Elements("AroundBeacon"))
                            {
                                var aroundBeaconItem = new AroundBeacon();
                                FillEntityWithXml(aroundBeaconItem, new XDocument(item));
                                aroundBeacons.Add(aroundBeaconItem);
                            }
                            prop.SetValue(entity, aroundBeacons, null);
                        }
                        else if (genericArgumentTypeName == "CopyrightCheckResult_ResultList")        //RequestMessageEvent_MassSendJobFinish
                        {
                            List <CopyrightCheckResult_ResultList> resultList = new List <CopyrightCheckResult_ResultList>();
                            foreach (var item in root.Elements("ResultList").Elements("item"))
                            {
                                CopyrightCheckResult_ResultList resultItem = new CopyrightCheckResult_ResultList();
                                FillEntityWithXml(resultItem.item, new XDocument(item));
                                resultList.Add(resultItem);
                            }
                            prop.SetValue(entity, resultList, null);
                        }
                        //企业微信
                        else if (genericArguments[0].Name == "MpNewsArticle")
                        {
                            List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>();
                            foreach (var item in root.Elements(propName))
                            {
                                var mpNewsArticle = new MpNewsArticle();
                                FillEntityWithXml(mpNewsArticle, new XDocument(item));
                                mpNewsArticles.Add(mpNewsArticle);
                            }
                            prop.SetValue(entity, mpNewsArticles, null);
                        }
                        break;
                    }

                    case "Music":    //ResponseMessageMusic适用
                        FillClassValue <Music>(entity, root, propName, prop);
                        break;

                    case "Image":    //ResponseMessageImage适用
                        FillClassValue <Image>(entity, root, propName, prop);
                        break;

                    case "Voice":    //ResponseMessageVoice适用
                        FillClassValue <Voice>(entity, root, propName, prop);
                        break;

                    case "Video":    //ResponseMessageVideo适用
                        FillClassValue <Video>(entity, root, propName, prop);
                        break;

                    case "ScanCodeInfo":    //扫码事件中的ScanCodeInfo适用
                        FillClassValue <ScanCodeInfo>(entity, root, propName, prop);
                        break;

                    case "SendLocationInfo":    //弹出地理位置选择器的事件推送中的SendLocationInfo适用
                        FillClassValue <SendLocationInfo>(entity, root, propName, prop);
                        break;

                    case "SendPicsInfo":    //系统拍照发图中的SendPicsInfo适用
                        FillClassValue <SendPicsInfo>(entity, root, propName, prop);
                        break;

                    case "ChosenBeacon":    //摇一摇事件通知
                        FillClassValue <ChosenBeacon>(entity, root, propName, prop);
                        break;

                    case "AroundBeacon":    //摇一摇事件通知
                        FillClassValue <AroundBeacon>(entity, root, propName, prop);
                        break;

                        #region 开放平台-小程序
                    case "ThirdFasteRegisterInfo":     //开放平台-小程序-快速注册
                        FillClassValue <ThirdFasteRegisterInfo>(entity, root, propName, prop);
                        break;
                        #endregion

                        #region RequestMessageEvent_MassSendJobFinish
                    case "CopyrightCheckResult":
                        FillClassValue <CopyrightCheckResult>(entity, root, propName, prop);
                        break;

                    case "CopyrightCheckResult_ResultList_Item":
                        FillClassValue <CopyrightCheckResult_ResultList_Item>(entity, root, "item", prop);
                        break;
                        #region 企业号

                        /* 暂时放在Work.dll中
                         *                      case "AgentType":
                         *                          {
                         *                              AgentType tp;
                         #if NET35
                         *                              try
                         *                              {
                         *                                  tp = (AgentType)Enum.Parse(typeof(AgentType), root.Element(propName).Value, true);
                         *                                  prop.SetValue(entity, tp, null);
                         *                              }
                         *                              catch
                         *                              {
                         *
                         *                              }
                         #else
                         *                              if (Enum.TryParse(root.Element(propName).Value, out tp))
                         *                              {
                         *                                  prop.SetValue(entity, tp, null);
                         *                              }
                         #endif
                         *                              break;
                         *                          }
                         *                      case "Receiver":
                         *                          {
                         *                              Receiver receiver = new Receiver();
                         *                              FillEntityWithXml(receiver, new XDocument(root.Element(propName)));
                         *                              prop.SetValue(entity, receiver, null);
                         *                              break;
                         *                          }
                         */
                        #endregion

                        #endregion

                    default:
                        var enumSuccess = false;
                        if (prop.PropertyType.IsEnum)
                        {
                            //未知的枚举类型
                            try
                            {
                                prop.SetValue(entity, Enum.Parse(prop.PropertyType, root.Element(propName).Value, true), null);
                                enumSuccess = true;
                            }
                            catch
                            {
                            }
                        }

                        if (!enumSuccess)
                        {
                            prop.SetValue(entity, root.Element(propName).Value, null);
                        }
                        break;
                    }
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// 根据XML信息填充实实体
        /// </summary>
        /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam>
        /// <param name="entity">实体</param>
        /// <param name="doc">XML</param>
        public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new()
        {
            entity = entity ?? new T();
            var root = doc.Root;

            if (root == null)
            {
                return;//无法处理
            }

            var props = entity.GetType().GetProperties();

            foreach (var prop in props)
            {
                var propName = prop.Name;
                if (root.Element(propName) != null)
                {
                    switch (prop.PropertyType.Name)
                    {
                    //case "String":
                    //    goto default;
                    case "DateTime":
                    case "Int32":
                    case "Int64":
                    case "Double":
                    case "Nullable`1":     //可为空对象
                        EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value);
                        break;

                    case "Boolean":
                        if (propName == "FuncFlag")
                        {
                            EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1");
                        }
                        else
                        {
                            goto default;
                        }
                        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 "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();
                                FillEntityWithXml(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();
                                FillEntityWithXml(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();
                                FillEntityWithXml(aroundBeaconItem, new XDocument(item));
                                aroundBeacons.Add(aroundBeaconItem);
                            }
                            prop.SetValue(entity, aroundBeacons, null);
                        }
                        break;
                    }

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

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

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

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

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

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

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

                    case "ChosenBeacon":    //摇一摇事件通知
                        ChosenBeacon chosenBeacon = new ChosenBeacon();
                        FillEntityWithXml(chosenBeacon, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, chosenBeacon, null);
                        break;

                    case "AroundBeacon":    //摇一摇事件通知
                        AroundBeacon aroundBeacon = new AroundBeacon();
                        FillEntityWithXml(aroundBeacon, new XDocument(root.Element(propName)));
                        prop.SetValue(entity, aroundBeacon, null);
                        break;

                    default:
                        prop.SetValue(entity, root.Element(propName).Value, null);
                        break;
                    }
                }
            }
        }