public static AbstractRequest GetRequestEntity(XDocument doc) { AbstractRequest abstractRequest; switch (MsgTypeHelper.GetMsgType(doc)) { case RequestMsgType.Text: abstractRequest = new TextRequest(); break; case RequestMsgType.Image: abstractRequest = new ImageRequest(); break; case RequestMsgType.Voice: abstractRequest = new VoiceRequest(); break; case RequestMsgType.Video: abstractRequest = new VideoRequest(); break; case RequestMsgType.Location: abstractRequest = new LocationRequest(); break; case RequestMsgType.Link: abstractRequest = new LinkRequest(); break; case RequestMsgType.Event: switch (EventTypeHelper.GetEventType(doc)) { case RequestEventType.Subscribe: abstractRequest = new SubscribeEventRequest(); break; case RequestEventType.UnSubscribe: abstractRequest = new UnSubscribeEventRequest(); break; case RequestEventType.Scan: abstractRequest = new ScanEventRequest(); break; case RequestEventType.Location: abstractRequest = new LocationEventRequest(); break; case RequestEventType.Click: abstractRequest = new ClickEventRequest(); break; default: throw new ArgumentOutOfRangeException(); } break; default: throw new ArgumentOutOfRangeException(); } EntityHelper.FillEntityWithXml <AbstractRequest>(abstractRequest, doc); return(abstractRequest); }
public static void FillEntityWithXml <T>(T entity, XDocument doc) where T : AbstractRequest, new() { T tEntity; if ((tEntity = entity) == null) { tEntity = Activator.CreateInstance <T>(); } entity = tEntity; XElement root = doc.Root; PropertyInfo[] properties = entity.GetType().GetProperties(); PropertyInfo[] array = properties; for (int i = 0; i < array.Length; i++) { PropertyInfo propertyInfo = array[i]; string propertyName = propertyInfo.Name; if (root.Element(propertyName) != null) { string propertyTypeName = propertyInfo.PropertyType.Name; if (propertyTypeName == null) { propertyInfo.SetValue(entity, root.Element(propertyName).Value, null); continue; } if (!(propertyTypeName == "DateTime")) { if (!(propertyTypeName == "Boolean")) { if (!(propertyTypeName == "Int64")) { if (!(propertyTypeName == "RequestEventType")) { if (!(propertyTypeName == "RequestMsgType")) { propertyInfo.SetValue(entity, root.Element(propertyName).Value, null); continue; } propertyInfo.SetValue(entity, MsgTypeHelper.GetMsgType(root.Element(propertyName).Value), null); } else { propertyInfo.SetValue(entity, EventTypeHelper.GetEventType(root.Element(propertyName).Value), null); } } else { propertyInfo.SetValue(entity, long.Parse(root.Element(propertyName).Value), null); } } else { if (!(propertyName == "FuncFlag")) { propertyInfo.SetValue(entity, root.Element(propertyName).Value, null); continue; } propertyInfo.SetValue(entity, root.Element(propertyName).Value == "1", null); } } else { propertyInfo.SetValue(entity, new DateTime(long.Parse(root.Element(propertyName).Value)), null); } } } }
public static RequestMsgType GetMsgType(XDocument doc) { return(MsgTypeHelper.GetMsgType(doc.Root.Element("MsgType").Value)); }