Exemplo n.º 1
0
        ///<summary>
        ///</summary>
        ///<param name = "type"></param>
        ///<returns></returns>
        public static TypeInfo GetTypeInfo(Type type, SimpleTypes st)
        {
            // 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 = st.IsSimple(type) || 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;
        }
 ///<summary>
 ///</summary>
 ///<param name = "reader"></param>
 public XmlPropertyDeserializer(IXmlReader reader, SimpleTypes st)
 {
     _reader = reader;
     _simpleTypes = st;
 }
Exemplo n.º 3
0
        ///<summary>
        ///</summary>
        ///<param name = "obj"></param>
        ///<returns></returns>
        ///<exception cref = "ArgumentNullException"></exception>
        public static TypeInfo GetTypeInfo(object obj, SimpleTypes st)
        {
            if (obj == null) throw new ArgumentNullException("obj");

            Type type = obj.GetType();
            return GetTypeInfo(type, st);
        }