private static void ProcessSimpleTypeConstraintAttribute(TypeConstraint constraints, PropertyInfo property) { SimpleTypeConstraintAttribute typeConstraint = property.GetCustomAttribute <SimpleTypeConstraintAttribute>(); if (typeConstraint == null) { return; } foreach (Type type in SimpleTypes.DotNetTypes) { if (!constraints.DotNetTypes.Contains(type)) { constraints.DotNetTypes.Add(type); } } }
/// <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); }