Exemple #1
0
        /// <summary>
        ///   Should the property be removed from serialization?
        /// </summary>
        /// <param name = "info"></param>
        /// <param name = "property"></param>
        /// <returns>
        ///   true if the property:
        ///   - is in the PropertiesToIgnore,
        ///   - contains ExcludeFromSerializationAttribute,
        ///   - does not have it's set or get accessor
        ///   - is indexer
        /// </returns>
        protected virtual bool IgnoreProperty(TypeInfo info, PropertyInfo property)
        {
            // Soll die Eigenschaft ignoriert werden
            if (PropertiesToIgnore.Contains(info.Type, property.Name))
            {
                return(true);
            }

            if (ContainsExcludeFromSerializationAttribute(property))
            {
                return(true);
            }

            if (!property.CanRead || !property.CanWrite)
            {
                return(true);
            }

            ParameterInfo[] indexParameters = property.GetIndexParameters();
            if (indexParameters.Length > 0)
            {
                // Indexer
                return(true);
            }

            return(false);
        }
Exemple #2
0
        /// <summary>
        ///   Gives all properties back which:
        ///   - are public
        ///   - are not static
        ///   - does not contain ExcludeFromSerializationAttribute
        ///   - have their set and get accessors
        ///   - are not indexers
        /// </summary>
        /// <param name = "typeInfo"></param>
        /// <returns></returns>
        public IList <PropertyInfo> GetProperties(TypeInfo typeInfo)
        {
            // Search in cache
            var propertyInfos = Cache.TryGetPropertyInfos(typeInfo.Type);

            if (propertyInfos != null)
            {
                return(propertyInfos);
            }

            // Creating infos
            PropertyInfo[] properties = GetAllProperties(typeInfo.Type);
            var            result     = new List <PropertyInfo>();

            foreach (PropertyInfo property in properties)
            {
                if (!IgnoreProperty(typeInfo, property))
                {
                    result.Add(property);
                }
            }

            // adding result to Cache
            Cache.Add(typeInfo.Type, result);

            return(result);
        }
Exemple #3
0
 public override bool IgnoreCollectionItems(Polenter.Serialization.Serializing.TypeInfo info)
 {
     return(!collectionItemHandlerCallback(info.Type));
 }
Exemple #4
0
 protected override bool IgnoreProperty(Polenter.Serialization.Serializing.TypeInfo info, PropertyInfo property)
 {
     return(!propertyHandlerCallback(info.Type, property));
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="typeInfo"></param>
        /// <param name="type"></param>
        /// <returns>true if the key and value definition was found</returns>
        private static bool fillKeyAndElementType(TypeInfo typeInfo, Type type)
        {
            if (type.IsGenericType)
            {
                Type[] arguments = type.GetGenericArguments();

                if (typeInfo.IsDictionary)
                {
                    // in Dictionary there are keys and values
                    typeInfo.KeyType = arguments[0];
                    typeInfo.ElementType = arguments[1];
                }
                else
                {
                    // In Collection there are only items
                    typeInfo.ElementType = arguments[0];
                }
                return arguments.Length > 0;
            }
            return false;
        }
        ///<summary>
        ///</summary>
        ///<param name = "type"></param>
        ///<returns></returns>
        public static TypeInfo GetTypeInfo(Type type)
        {
            // check if Info is in cache
            TypeInfo typeInfo = Cache.TryGetTypeInfo(type);
            if (typeInfo == null)
            {
                // no info in cache yet
                typeInfo = new TypeInfo();
                typeInfo.Type = type;

                typeInfo.IsSimple = Tools.IsSimple(type);   

                // new since v.2.16
                // check if array of byte
                if (type==typeof(byte[]))
                {
                    typeInfo.ElementType = typeof (byte);
                }

                // Only not simple types can be Collections
                if (!typeInfo.IsSimple)
                {
                    // check if it is an Array
                    typeInfo.IsArray = Tools.IsArray(type);

                    if (typeInfo.IsArray)
                    {
                        // Array? What is its element type?
                        if (type.HasElementType)
                        {
                            typeInfo.ElementType = type.GetElementType();
                        }

                        // How many dimensions
                        typeInfo.ArrayDimensionCount = type.GetArrayRank();
                    }
                    else
                    {
                        // It is not Array, maybe Enumerable?
                        typeInfo.IsEnumerable = Tools.IsEnumerable(type);
                        if (typeInfo.IsEnumerable)
                        {
                            // it is Enumerable maybe Collection?
                            typeInfo.IsCollection = Tools.IsCollection(type);

                            if (typeInfo.IsCollection)
                            {
                                // Sure it is a Collection, but maybe Dictionary also?
                                typeInfo.IsDictionary = Tools.IsDictionary(type);

                                // Fill its key and value types, if the listing is generic
                                bool elementTypeDefinitionFound;
                                var examinedType = type;
                                do
                                {
                                    elementTypeDefinitionFound = fillKeyAndElementType(typeInfo, examinedType);
                                    examinedType = examinedType.BaseType;
                                    // until key and element definition was found, or the base typ is an object
                                } while (!elementTypeDefinitionFound && examinedType!=null && examinedType!=typeof(object));
                            }
                        }
                    }
                }
#if Smartphone
                Cache.AddIfNotExists(typeInfo);
#else
                Cache.Add(typeInfo);
#endif
            }

            return typeInfo;
        }
Exemple #7
0
 public virtual bool IgnoreCollectionItems(TypeInfo info)
 {
     return(false);
 }