Ejemplo n.º 1
0
        private IEnumerable <ElementInfo> ExtractPropertyElements(TypeInfo typeInfo)
        {
            bool IsSerializableProperty(PropertyInfo property)
            {
                return(property.CanRead &&
                       !property.GetMethod.IsStatic &&
                       (property.CanWrite || IsEnumerable(property)));
            }

            foreach (PropertyInfo property in typeInfo.DeclaredProperties.Where(IsSerializableProperty))
            {
                KmlAttributeAttribute attribute = GetAttribute <KmlAttributeAttribute>(property);
                if (attribute != null)
                {
                    var component = new XmlComponent(null, attribute.AttributeName, null);

                    // Check if a property has already been registered with the info.
                    // Ignore later properties - i.e. don't throw an exception.
                    if (!this.attributes.ContainsKey(component))
                    {
                        this.attributes.Add(component, new ElementInfo(property, attribute));
                    }
                }
                else
                {
                    KmlElementAttribute kmlElement = GetElement(property);
                    if (kmlElement != null)
                    {
                        yield return(new ElementInfo(property, kmlElement));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static void RegisterElement(Type type)
        {
            KmlElementAttribute element = TypeBrowser.GetElement(type.GetTypeInfo());

            if (element != null)
            {
                var xml = new XmlComponent(null, element.ElementName, element.Namespace);
                RegisterType(xml, type);
            }
        }
Ejemplo n.º 3
0
        private static string GetString(object value)
        {
            Type type = value.GetType();

            if (type.IsEnum)
            {
                KmlElementAttribute att = TypeBrowser.GetEnum((Enum)value);
                if (att != null)
                {
                    return(att.ElementName);
                }
            }
            return(string.Format(KmlFormatter.Instance, "{0}", value));
        }
Ejemplo n.º 4
0
 private static void RegisterAssembly(Assembly assembly)
 {
     foreach (var type in assembly.GetExportedTypes())
     {
         if (type.IsSubclassOf(typeof(Element)))
         {
             KmlElementAttribute element = TypeBrowser.GetElement(type);
             if (element != null)
             {
                 var xml = new XmlComponent(null, element.ElementName, element.Namespace);
                 RegisterType(xml, type);
             }
         }
     }
 }
Ejemplo n.º 5
0
        private static Dictionary <string, object> GetEnumValues(TypeInfo enumType)
        {
            var lookup = new Dictionary <string, object>(StringComparer.Ordinal);

            foreach (FieldInfo field in enumType.DeclaredFields.Where(f => f.IsStatic))
            {
                KmlElementAttribute element = TypeBrowser.GetElement(field);
                if (element != null)
                {
                    lookup.Add(element.ElementName, field.GetValue(null));
                }
            }

            return(lookup);
        }
Ejemplo n.º 6
0
 private static object GetEnum(Type type, string value)
 {
     if (value != null)
     {
         value = value.Trim();
         foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static))
         {
             KmlElementAttribute element = TypeBrowser.GetElement(field);
             if (element != null && string.Equals(element.ElementName, value, StringComparison.Ordinal))
             {
                 return(field.GetValue(null));
             }
         }
     }
     return(null);
 }
Ejemplo n.º 7
0
        private static object GetEnum(TypeInfo typeInfo, string value)
        {
            if (value != null)
            {
                value = value.Trim();
                foreach (FieldInfo field in typeInfo.DeclaredFields.Where(f => f.IsStatic))
                {
                    KmlElementAttribute element = TypeBrowser.GetElement(field);
                    if (element != null && string.Equals(element.ElementName, value, StringComparison.Ordinal))
                    {
                        return(field.GetValue(null));
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 8
0
        private void ExtractAttributes(Type type)
        {
            if (type == null || type == typeof(object))
            {
                return; // We've reached the top, now we have to stop
            }

            // Look at the base type first as the KML schema specifies <sequence>
            // This will also find private fields in the base classes, which can't
            // be seen through a derived class.
            TypeInfo typeInfo = type.GetTypeInfo();

            this.ExtractAttributes(typeInfo.BaseType);

            // Store the found elements here so we can add them in order later
            var elements = new List <Tuple <XmlComponent, PropertyInfo, KmlElementAttribute> >();

            foreach (PropertyInfo property in typeInfo.DeclaredProperties.Where(p => !p.GetMethod.IsStatic))
            {
                KmlAttributeAttribute attribute = GetAttribute(property);
                if (attribute != null)
                {
                    var component = new XmlComponent(null, attribute.AttributeName, null);

                    // Check if a property has already been registered with the info.
                    // Ignore later properties - i.e. don't throw an exception.
                    if (!this.attributes.ContainsKey(component))
                    {
                        this.attributes.Add(component, Tuple.Create(property, attribute));
                    }
                }
                else
                {
                    KmlElementAttribute element = GetElement(property);
                    if (element != null)
                    {
                        var component = new XmlComponent(null, element.ElementName, element.Namespace);
                        elements.Add(Tuple.Create(component, property, element));
                    }
                }
            }

            // Now add the elements in order
            this.elements.AddRange(elements.OrderBy((Tuple <XmlComponent, PropertyInfo, KmlElementAttribute> e) => e.Item3.Order));
        }
Ejemplo n.º 9
0
        private string GetString(object value)
        {
            TypeInfo typeInfo = value.GetType().GetTypeInfo();

            if (typeInfo.IsEnum)
            {
                KmlElementAttribute att = TypeBrowser.GetEnum((Enum)value);
                if (att != null)
                {
                    return(att.ElementName);
                }
            }

            if (((this.Options & SerializerOptions.ReadableFloatingPoints) != 0) &&
                (value is double || value is float))
            {
                return(string.Format(KmlFormatter.Instance, "{0:G}", value));
            }
            else
            {
                return(string.Format(KmlFormatter.Instance, "{0}", value));
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ElementInfo"/> class.
 /// </summary>
 /// <param name="property">The property information.</param>
 /// <param name="kmlElement">The KML element information.</param>
 public ElementInfo(PropertyInfo property, KmlElementAttribute kmlElement)
     : this(property)
 {
     this.Component = new XmlComponent(null, kmlElement.ElementName, kmlElement.Namespace);
     this.Order     = kmlElement.Order;
 }