Exemplo n.º 1
0
        ///<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 PORTABLE
                Cache.AddIfNotExists(typeInfo);
#else
                Cache.Add(typeInfo);
#endif
            }

            return typeInfo;
        }
Exemplo n.º 2
0
        /// <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;
        }