예제 #1
0
 private static void SetProperties(XElement rootElement, Ticket ticket)
 {
     var properties = ticket.GetType().GetProperties();
     foreach (var prop in properties)
     {
         var attribute = prop.CustomAttributes.First(x => x.AttributeType == typeof (XmlElementAttribute));
         if (attribute != null)
         {
             var elementName = attribute.ConstructorArguments.First().Value as string;
             var element = rootElement.Element(elementName);
             if (element != null)
             {
                 var elementType = element.Attribute("type");
                 if(elementType != null && GetTypeFromName(elementType.Value) == prop.PropertyType)
                 {
                     if (prop.PropertyType == typeof (int))
                     {
                         prop.SetValue(ticket, Convert.ToInt32(element.Value));
                     }
                     else if (prop.PropertyType == typeof (bool))
                     {
                         prop.SetValue(ticket, Convert.ToBoolean(element.Value));
                     }
                     else if (prop.PropertyType == typeof (DateTime))
                     {
                         prop.SetValue(ticket, Convert.ToDateTime(element.Value));
                     }
                 }
                 else if(elementType == null && prop.PropertyType == typeof(string)){
                     prop.SetValue(ticket, element.Value);
                 }
                 else
                 {
                     throw new StrongTypingException(elementName);
                 }
             }
         }
     }
 }