public void Validate(IFieldPropertyInfo property, Type parentType, AbstractAttributeContext context) { XmlPropertyAttributeContext xmlContext = (XmlPropertyAttributeContext)context; Type type = property.Type; if (xmlContext.HasXmlFlattenHierarchyAttribute && !type.IsClass) { throw InvalidXmlAttributeException.Create(parentType, property, typeof(XmlFlattenHierarchyAttribute), FLATTEN_HIERARCHY_REASON_ERROR); } if (xmlContext.HasXmlListAttribute && !type.IsCollection()) { throw InvalidXmlAttributeException.Create(parentType, property, typeof(XmlListAttribute), LIST_REASON_ERROR); } if (xmlContext.HasXmlDictionaryAttribute && !type.IsDictionary()) { throw InvalidXmlAttributeException.Create(parentType, property, typeof(XmlDictionaryAttribute), DICTIONARY_REASON_ERROR); } if (!xmlContext.HasXmlDictionaryAttribute && type.IsDictionary() && type.GenericTypeArguments[0].Equals(typeof(String))) { throw InvalidXmlMappingException.Create(parentType, property, DICTIONARY_AUTOMAP_REASON_ERROR); } }
public ClassMetaData(Type type) { _classType = type; _classAttributeContext = new ClassAttributeContext(type); _properties = type.GetFieldsAndProperties(ReflectionUtils.GetPublicBindingFlags()); _propertyContexts = new ConcurrentDictionary <IFieldPropertyInfo, ConcurrentDictionaryTypeContext>(); foreach (IFieldPropertyInfo property in _properties) { ConcurrentDictionaryTypeContext propertyDictionary = new ConcurrentDictionaryTypeContext(); foreach (MetaDataEntry entry in MetaDataRegistry.Entries) { if (ReflectionUtils.HasCustomAttributeType(entry.Interface, property)) { AbstractAttributeContext context = (AbstractAttributeContext)Activator.CreateInstance(entry.Type, new object[] { property }); if (entry.Validator != null) { IPropertyAttributeValidator validatorInstance = (IPropertyAttributeValidator)Activator.CreateInstance(entry.Validator); validatorInstance.Validate(property, _classType, context); } if (!propertyDictionary.TryAdd(entry.Type, context)) { throw new ReflectionCacheException($"Type {type.AssemblyQualifiedName} could not be added to the cache because property {property.Name} could not be added to cache"); } } } if (!_propertyContexts.TryAdd(property, propertyDictionary)) { throw new ReflectionCacheException($"Type {type.AssemblyQualifiedName} could not be added to the cache"); } } }
public static T GetCustomAttribute <T>(AbstractAttributeContext context) { return((T)context.Attributes .Where((IAttributeMarker attribute) => attribute is T) .FirstOrDefault()); }