public TypeMetadata(Type entityType) { Type type = TypeUtility.GetElementType(entityType); ClrType = type; IEnumerable <PropertyDescriptor> properties = TypeDescriptor.GetProperties(type).Cast <PropertyDescriptor>().OrderBy(p => p.Name) .Where(p => TypeUtility.IsDataMember(p)); foreach (PropertyDescriptor pd in properties) { _properties.Add(new TypePropertyMetadata(pd)); if (TypeDescriptorExtensions.ExplicitAttributes(pd)[typeof(KeyAttribute)] != null) { _key.Add(pd.Name); } } }
public TypePropertyMetadata(PropertyDescriptor descriptor) { Name = descriptor.Name; Type elementType = TypeUtility.GetElementType(descriptor.PropertyType); IsArray = !elementType.Equals(descriptor.PropertyType); // TODO: What should we do with nullable types here? ClrType = elementType; AttributeCollection propertyAttributes = TypeDescriptorExtensions.ExplicitAttributes(descriptor); // TODO, 336102, ReadOnlyAttribute for editability? RIA used EditableAttribute? ReadOnlyAttribute readonlyAttr = (ReadOnlyAttribute)propertyAttributes[typeof(ReadOnlyAttribute)]; IsReadOnly = (readonlyAttr != null) ? readonlyAttr.IsReadOnly : false; AssociationAttribute associationAttr = (AssociationAttribute)propertyAttributes[typeof(AssociationAttribute)]; if (associationAttr != null) { Association = new TypePropertyAssociationMetadata(associationAttr); } RequiredAttribute requiredAttribute = (RequiredAttribute)propertyAttributes[typeof(RequiredAttribute)]; if (requiredAttribute != null) { _validationRules.Add(new TypePropertyValidationRuleMetadata(requiredAttribute)); } RangeAttribute rangeAttribute = (RangeAttribute)propertyAttributes[typeof(RangeAttribute)]; if (rangeAttribute != null) { Type operandType = rangeAttribute.OperandType; operandType = Nullable.GetUnderlyingType(operandType) ?? operandType; if (operandType.Equals(typeof(Double)) || operandType.Equals(typeof(Int16)) || operandType.Equals(typeof(Int32)) || operandType.Equals(typeof(Int64)) || operandType.Equals(typeof(Single))) { _validationRules.Add(new TypePropertyValidationRuleMetadata(rangeAttribute)); } } StringLengthAttribute stringLengthAttribute = (StringLengthAttribute)propertyAttributes[typeof(StringLengthAttribute)]; if (stringLengthAttribute != null) { _validationRules.Add(new TypePropertyValidationRuleMetadata(stringLengthAttribute)); } DataTypeAttribute dataTypeAttribute = (DataTypeAttribute)propertyAttributes[typeof(DataTypeAttribute)]; if (dataTypeAttribute != null) { if (dataTypeAttribute.DataType.Equals(DataType.EmailAddress) || dataTypeAttribute.DataType.Equals(DataType.Url)) { _validationRules.Add(new TypePropertyValidationRuleMetadata(dataTypeAttribute)); } } }