Beispiel #1
0
        /// <summary>
        /// This method extract TypeMetadata object from Type
        /// </summary>
        /// <param name="type">type that need to be processed</param>
        /// <returns>TypeMetadata object that is representing given type</returns>
        public static TypeMetadata ExtractTypeMetadata(Type type)
        {
            TypeMetadata typeMetadata   = new TypeMetadata();
            Type         collectionType = null;
            Type         dictionaryType = null;

            typeMetadata.Name             = type.Name;
            typeMetadata.ID               = type.GUID;
            typeMetadata.IsScalar         = ScalarTypes.IsScalarType(type);
            typeMetadata.IsInterface      = type.IsInterface;
            typeMetadata.IsGenericType    = type.IsGenericType;
            typeMetadata.IsCollectionType = Utils.IsCollectionType(type, ref collectionType);
            typeMetadata.IsDictionaryType = Utils.IsDictionaryType(type, ref dictionaryType);
            typeMetadata.IsEnum           = type.IsEnum;
            typeMetadata.EnumValues       = ExtractEnumValues(type);
            typeMetadata.GenericType      = type.IsGenericType? type.GetGenericTypeDefinition().Name : null;
            typeMetadata.CustomAttributes = ExtractCustomAttribute(type);
            typeMetadata.Interfaces       = ExtractInterfaces(type);
            typeMetadata.GenericArguments = ExtractGenericAttributes(type);
            if (!typeMetadata.IsScalar && !typeMetadata.IsDictionaryType && !typeMetadata.IsCollectionType)
            {
                typeMetadata.Properties = ExtractPropertiesMetadata(type);
            }


            return(typeMetadata);
        }
Beispiel #2
0
        /// <summary>
        /// This method is used for finding all property types. It is used in way, in begining you should
        /// give this method RootType as type, and return list will contain all types that root type contains.
        /// </summary>
        /// <param name="type"> type that need to be processed</param>
        /// <param name="result"> list in which we will add all types that are found</param>
        /// <returns>list of all finded types</returns>
        public static List <Type> RecursivelyExtractTypeMetadata(Type type, List <Type> result)
        {
            if (type.GetInterfaces() != null && type.GetInterfaces().Length != 0)
            {
                foreach (Type t in type.GetInterfaces())
                {
                    if (!result.Contains(t))
                    {
                        RecursivelyExtractTypeMetadata(t, result);
                    }
                    if (!result.Contains(t))
                    {
                        result.Add(t);
                    }
                }
            }

            Collection <PropertyInfo> propertiesOfType = new Collection <PropertyInfo>();

            Utils.ExtractProperties(type, propertiesOfType);
            result.Insert(0, type);
            foreach (PropertyInfo pi in propertiesOfType)
            {
                Type propertyType   = null;
                Type collectionType = null;
                Type dictionaryType = null;

                if (Utils.IsCollectionType(pi.PropertyType, ref collectionType))
                {
                    if (collectionType.IsGenericType)
                    {
                        propertyType = collectionType.GetGenericArguments()[0];
                        if (!result.Contains(propertyType) && !ScalarTypes.IsScalarType(propertyType))
                        {
                            TypeSingleton.RecursivelyExtractTypeMetadata(propertyType, result);
                        }

                        result.Add(collectionType);
                    }
                }
                else
                {
                    if (Utils.IsDictionaryType(pi.PropertyType, ref dictionaryType))
                    {
                        Type firstType  = dictionaryType.GetGenericArguments()[0];
                        Type secondType = dictionaryType.GetGenericArguments()[1];

                        if (!result.Contains(firstType) && !ScalarTypes.IsScalarType(propertyType))
                        {
                            TypeSingleton.RecursivelyExtractTypeMetadata(firstType, result);
                        }
                        if (!result.Contains(secondType) && !ScalarTypes.IsScalarType(propertyType))
                        {
                            TypeSingleton.RecursivelyExtractTypeMetadata(secondType, result);
                        }

                        result.Add(dictionaryType);
                    }
                    else
                    {
                        propertyType = pi.PropertyType;
                        if (!result.Contains(propertyType) && !ScalarTypes.IsScalarType(propertyType))
                        {
                            TypeSingleton.RecursivelyExtractTypeMetadata(propertyType, result);
                        }
                    }
                }
            }


            return(result);
        }