Beispiel #1
0
        private static void ProcessTypeConstraintAttribute(TypeConstraint constraints, IEnumerable <LanguageConcept> languages, PropertyInfo property)
        {
            TypeConstraintAttribute typeConstraint = property.GetCustomAttribute <TypeConstraintAttribute>();

            if (typeConstraint == null)
            {
                return;
            }
            ClassifyTypeConstraints(constraints, languages, typeConstraint.Types);
        }
        /// <summary>
        /// Gets property type constraints for the given concept's property (derived from SyntaxNode only)
        /// </summary>
        public static IEnumerable <ISyntaxNode> GetPropertyTypeConstraints(ISyntaxNode concept, string propertyName)
        {
            List <ISyntaxNode> constraints = new List <ISyntaxNode>();

            Type                          metadata             = concept.GetType();
            PropertyInfo                  property             = metadata.GetProperty(propertyName);
            TypeConstraintAttribute       typeConstraint       = property.GetCustomAttribute <TypeConstraintAttribute>();
            SimpleTypeConstraintAttribute simpleTypeConstraint = property.GetCustomAttribute <SimpleTypeConstraintAttribute>();

            if (simpleTypeConstraint != null)
            {
                constraints.AddRange(SimpleTypes.References);
            }
            if (typeConstraint != null)
            {
                foreach (Type type in typeConstraint.Types)
                {
                    if (type.IsAbstract)
                    {
                        foreach (Type subclass in GetSubclassesOfType(type))
                        {
                            constraints.Add((ISyntaxNode)Activator.CreateInstance(subclass));
                        }
                    }
                    else
                    {
                        if (type.IsClass && type.IsSubclassOf(typeof(SyntaxNode)))
                        {
                            constraints.Add((ISyntaxNode)Activator.CreateInstance(type));
                        }
                    }
                }
            }

            if (property.IsOptional())
            {
                property = property.PropertyType.GetProperty("Value");
            }
            Type propertyType;

            if (property.IsList()) // looking for List<T>
            {
                propertyType = property.PropertyType.GenericTypeArguments[0];
            }
            else
            {
                propertyType = property.PropertyType;
            }

            if (propertyType.IsAbstract)
            {
                foreach (Type subclass in GetSubclassesOfType(propertyType))
                {
                    constraints.Add((ISyntaxNode)Activator.CreateInstance(subclass));
                }
            }
            else
            {
                if (propertyType.IsClass && propertyType.IsSubclassOf(typeof(SyntaxNode)))
                {
                    constraints.Add((ISyntaxNode)Activator.CreateInstance(propertyType));
                }
            }

            return(constraints);
        }