/// <summary> /// Processes the specified attribute which is an instance of <c>YAXAttribute</c>. /// </summary> /// <param name="attr">The attribute to process.</param> private void ProcessYaxAttribute(object attr) { if (attr is YAXCommentAttribute) { string comment = (attr as YAXCommentAttribute).Comment; if (!String.IsNullOrEmpty(comment)) { string[] comments = comment.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < comments.Length; i++) { comments[i] = String.Format(" {0} ", comments[i].Trim()); } this.Comment = comments; } } else if (attr is YAXSerializableFieldAttribute) { this.IsAttributedAsSerializable = true; } else if (attr is YAXAttributeForClassAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(this.MemberType) || this.CustomSerializerType != null || (this.m_collectionAttributeInstance != null && this.m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially)) { this.IsSerializedAsAttribute = true; this.SerializationLocation = "."; } } else if (attr is YAXValueForClassAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(this.MemberType) || this.CustomSerializerType != null || (this.m_collectionAttributeInstance != null && this.m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially)) { this.IsSerializedAsValue = true; this.SerializationLocation = "."; } } else if (attr is YAXAttributeForAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(this.MemberType) || this.CustomSerializerType != null || (this.m_collectionAttributeInstance != null && this.m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially)) { this.IsSerializedAsAttribute = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXAttributeForAttribute).Parent, out path, out alias); this.SerializationLocation = path; if (!String.IsNullOrEmpty(alias)) { this.Alias = StringUtils.RefineSingleElement(alias); } } } else if (attr is YAXElementForAttribute) { this.IsSerializedAsElement = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXElementForAttribute).Parent, out path, out alias); this.SerializationLocation = path; if (!String.IsNullOrEmpty(alias)) { this.Alias = StringUtils.RefineSingleElement(alias); } } else if (attr is YAXValueForAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(this.MemberType) || this.CustomSerializerType != null || (this.m_collectionAttributeInstance != null && this.m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially)) { this.IsSerializedAsValue = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXValueForAttribute).Parent, out path, out alias); this.SerializationLocation = path; if (!String.IsNullOrEmpty(alias)) { this.Alias = StringUtils.RefineSingleElement(alias); } } } else if (attr is YAXDontSerializeAttribute) { this.IsAttributedAsDontSerialize = true; } else if (attr is YAXSerializeAsAttribute) { this.Alias = StringUtils.RefineSingleElement((attr as YAXSerializeAsAttribute).SerializeAs); } else if (attr is YAXCollectionAttribute) { this.m_collectionAttributeInstance = attr as YAXCollectionAttribute; } else if (attr is YAXDictionaryAttribute) { this.m_dictionaryAttributeInstance = attr as YAXDictionaryAttribute; } else if (attr is YAXErrorIfMissedAttribute) { var temp = attr as YAXErrorIfMissedAttribute; this.DefaultValue = temp.DefaultValue; this.TreatErrorsAs = temp.TreatAs; } else if (attr is YAXFormatAttribute) { this.Format = (attr as YAXFormatAttribute).Format; } else if (attr is YAXNotCollectionAttribute) { // arrays are always treated as collections if (!ReflectionUtils.IsArray(this.MemberType)) { this.IsAttributedAsNotCollection = true; } } else if (attr is YAXCustomSerializerAttribute) { Type serType = (attr as YAXCustomSerializerAttribute).CustomSerializerType; Type genTypeArg; bool isDesiredInterface = ReflectionUtils.IsDerivedFromGenericInterfaceType(serType, typeof(ICustomSerializer <>), out genTypeArg); if (!isDesiredInterface) { throw new YAXException("The provided custom serialization type is not derived from the proper interface"); } else if (genTypeArg != this.MemberType) { throw new YAXException("The generic argument of the class and the member type do not match"); } else { this.CustomSerializerType = serType; } } else if (attr is YAXPreserveWhitespaceAttribute) { this.PreservesWhitespace = true; } else if (attr is YAXSerializableTypeAttribute) { // this should not happen throw new Exception("This attribute is not applicable to fields and properties!"); } else if (attr is YAXNamespaceAttribute) { var nsAttrib = (attr as YAXNamespaceAttribute); this.Namespace = nsAttrib.Namespace; this.NamespacePrefix = nsAttrib.Prefix; } else if (attr is YAXTypeAttribute) { var yaxTypeAttr = attr as YAXTypeAttribute; string alias = yaxTypeAttr.Alias; if (alias != null) { alias = alias.Trim(); if (alias.Length == 0) { alias = null; } } if (this.m_possibleRealTypes.Any(x => x.Type == yaxTypeAttr.Type)) { throw new YAXPolymorphicException(String.Format("The type \"{0}\" for field/property \"{1}\" has already been defined through another attribute.", yaxTypeAttr.Type.Name, this.m_memberInfo)); } if (alias != null && this.m_possibleRealTypes.Any(x => alias.Equals(x.Alias, StringComparison.Ordinal))) { throw new YAXPolymorphicException(String.Format("The alias \"{0}\" given to type \"{1}\" for field/property \"{2}\" has already been given to another type through another attribute.", alias, yaxTypeAttr.Type.Name, this.m_memberInfo)); } this.m_possibleRealTypes.Add(yaxTypeAttr); } else if (attr is YAXCollectionItemTypeAttribute) { var yaxColletionItemTypeAttr = attr as YAXCollectionItemTypeAttribute; string alias = yaxColletionItemTypeAttr.Alias; if (alias != null) { alias = alias.Trim(); if (alias.Length == 0) { alias = null; } } if (this.m_possibleCollectionItemRealTypes.Any(x => x.Type == yaxColletionItemTypeAttr.Type)) { throw new YAXPolymorphicException(String.Format("The collection-item type \"{0}\" for collection \"{1}\" has already been defined through another attribute.", yaxColletionItemTypeAttr.Type.Name, this.m_memberInfo)); } if (alias != null && this.m_possibleCollectionItemRealTypes.Any(x => alias.Equals(x.Alias, StringComparison.Ordinal))) { throw new YAXPolymorphicException(String.Format("The alias \"{0}\" given to collection-item type \"{1}\" for field/property \"{2}\" has already been given to another type through another attribute.", alias, yaxColletionItemTypeAttr.Type.Name, this.m_memberInfo)); } this.m_possibleCollectionItemRealTypes.Add(yaxColletionItemTypeAttr); } else if (attr is YAXDontSerializeIfNullAttribute) { this.IsAttributedAsDontSerializeIfNull = true; } else if (attr is YAXElementOrder) { this.Order = (attr as YAXElementOrder).Order; } else { throw new Exception("Added new attribute type to the library but not yet processed!"); } }