public static RefineSingleElement ( string elemName ) : string | ||
elemName | string | Name of the element. |
return | string |
/// <summary> /// Initializes a new instance of the <see cref="MemberWrapper"/> class. /// </summary> /// <param name="memberInfo">The member-info to build this instance from.</param> /// <param name="callerSerializer">The caller serializer.</param> public MemberWrapper(MemberInfo memberInfo, YAXSerializer callerSerializer) { if (!(memberInfo.MemberType == MemberTypes.Property || memberInfo.MemberType == MemberTypes.Field)) { throw new Exception("Member must be either property or field"); } m_memberInfo = memberInfo; m_isProperty = (memberInfo.MemberType == MemberTypes.Property); Alias = StringUtils.RefineSingleElement(m_memberInfo.Name); if (m_isProperty) { m_propertyInfoInstance = (PropertyInfo)memberInfo; } else { m_fieldInfoInstance = (FieldInfo)memberInfo; } m_memberType = m_isProperty ? m_propertyInfoInstance.PropertyType : m_fieldInfoInstance.FieldType; m_memberTypeWrapper = TypeWrappersPool.Pool.GetTypeWrapper(MemberType, callerSerializer); InitInstance(); TreatErrorsAs = callerSerializer != null ? callerSerializer.DefaultExceptionType : YAXExceptionTypes.Error; // discovver YAXCustomSerializerAttributes earlier, because some other attributes depend on it var attrsToProcessEarlier = new HashSet <Type> { typeof(YAXCustomSerializerAttribute), typeof(YAXCollectionAttribute) }; foreach (var attrType in attrsToProcessEarlier) { var customSerAttrs = m_memberInfo.GetCustomAttributes(attrType, true); foreach (var attr in customSerAttrs) { ProcessYaxAttribute(attr); } } foreach (var attr in m_memberInfo.GetCustomAttributes(false)) { // no need to preces, it has been proccessed earlier if (attrsToProcessEarlier.Contains(attr.GetType())) { continue; } //if (attr is YAXCustomSerializerAttribute) // continue; // no need to preces, it has been proccessed earlier if (attr is YAXBaseAttribute) { ProcessYaxAttribute(attr); } } }
/// <summary> /// Initializes a new instance of the <see cref="UdtWrapper"/> class. /// </summary> /// <param name="udtType">The underlying type to create the wrapper around.</param> /// <param name="callerSerializer">reference to the serializer /// instance which is building this instance.</param> public UdtWrapper(Type udtType, YAXSerializer callerSerializer) { this.m_isTypeDictionary = false; this.m_udtType = udtType; this.m_isTypeCollection = ReflectionUtils.IsCollectionType(this.m_udtType); this.m_isTypeDictionary = ReflectionUtils.IsIDictionary(this.m_udtType); this.Alias = StringUtils.RefineSingleElement(ReflectionUtils.GetTypeFriendlyName(this.m_udtType)); this.Comment = null; this.FieldsToSerialize = YAXSerializationFields.PublicPropertiesOnly; this.IsAttributedAsNotCollection = false; this.SetYAXSerializerOptions(callerSerializer); foreach (var attr in this.m_udtType.GetCustomAttributes(true)) { if (attr is YAXBaseAttribute) { this.ProcessYAXAttribute(attr); } } }
/// <summary> /// Initializes a new instance of the <see cref="MemberWrapper" /> class. /// </summary> /// <param name="memberInfo">The member-info to build this instance from.</param> /// <param name="callerSerializer">The caller serializer.</param> public MemberWrapper(MemberInfo memberInfo, YAXSerializer callerSerializer) { Order = int.MaxValue; if (!(memberInfo.MemberType == MemberTypes.Property || memberInfo.MemberType == MemberTypes.Field)) { throw new Exception("Member must be either property or field"); } _memberInfo = memberInfo; _isProperty = memberInfo.MemberType == MemberTypes.Property; Alias = StringUtils.RefineSingleElement(_memberInfo.Name); if (_isProperty) { _propertyInfoInstance = (PropertyInfo)memberInfo; } else { _fieldInfoInstance = (FieldInfo)memberInfo; } _memberType = _isProperty ? _propertyInfoInstance.PropertyType : _fieldInfoInstance.FieldType; _memberTypeWrapper = TypeWrappersPool.Pool.GetTypeWrapper(MemberType, callerSerializer); if (_memberTypeWrapper.HasNamespace) { Namespace = _memberTypeWrapper.Namespace; NamespacePrefix = _memberTypeWrapper.NamespacePrefix; } InitInstance(); TreatErrorsAs = callerSerializer != null ? callerSerializer.DefaultExceptionType : YAXExceptionTypes.Error; // discover YAXCustomSerializerAttributes earlier, because some other attributes depend on it var attrsToProcessEarlier = new HashSet <Type> { typeof(YAXCustomSerializerAttribute), typeof(YAXCollectionAttribute) }; foreach (var attrType in attrsToProcessEarlier) { var customSerAttrs = Attribute.GetCustomAttributes(_memberInfo, attrType, true); foreach (var attr in customSerAttrs) { ProcessYaxAttribute(attr); } } foreach (var attr in Attribute.GetCustomAttributes(_memberInfo, true)) { // no need to process, it has been processed earlier if (attrsToProcessEarlier.Contains(attr.GetType())) { continue; } if (attr is YAXBaseAttribute) { ProcessYaxAttribute(attr); } } // now override some values from member-type-wrapper into member-wrapper // if member-type has collection attributes while the member itself does not have them, // then use those of the member-type if (_collectionAttributeInstance == null && _memberTypeWrapper.CollectionAttributeInstance != null) { _collectionAttributeInstance = _memberTypeWrapper.CollectionAttributeInstance; } _memberInfo.GetCustomAttributes(true); if (_dictionaryAttributeInstance == null && _memberTypeWrapper.DictionaryAttributeInstance != null) { _dictionaryAttributeInstance = _memberTypeWrapper.DictionaryAttributeInstance; } }
/// <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) { var comment = (attr as YAXCommentAttribute).Comment; if (!string.IsNullOrEmpty(comment)) { var comments = comment.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); for (var i = 0; i < comments.Length; i++) { comments[i] = string.Format(" {0} ", comments[i].Trim()); } Comment = comments; } } else if (attr is YAXSerializableFieldAttribute) { IsAttributedAsSerializable = true; } else if (attr is YAXAttributeForClassAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(MemberType) || HasCustomSerializer || MemberTypeWrapper.HasCustomSerializer || _collectionAttributeInstance != null && _collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially) { IsSerializedAsAttribute = true; SerializationLocation = "."; } } else if (attr is YAXValueForClassAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(MemberType) || HasCustomSerializer || MemberTypeWrapper.HasCustomSerializer || _collectionAttributeInstance != null && _collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially) { IsSerializedAsValue = true; SerializationLocation = "."; } } else if (attr is YAXAttributeForAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(MemberType) || HasCustomSerializer || _collectionAttributeInstance != null && _collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially) { IsSerializedAsAttribute = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXAttributeForAttribute).Parent, out path, out alias); SerializationLocation = path; if (!string.IsNullOrEmpty(alias)) { Alias = StringUtils.RefineSingleElement(alias); } } } else if (attr is YAXElementForAttribute) { IsSerializedAsElement = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXElementForAttribute).Parent, out path, out alias); SerializationLocation = path; if (!string.IsNullOrEmpty(alias)) { Alias = StringUtils.RefineSingleElement(alias); } } else if (attr is YAXValueForAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(MemberType) || HasCustomSerializer || _collectionAttributeInstance != null && _collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially) { IsSerializedAsValue = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXValueForAttribute).Parent, out path, out alias); SerializationLocation = path; if (!string.IsNullOrEmpty(alias)) { Alias = StringUtils.RefineSingleElement(alias); } } } else if (attr is YAXDontSerializeAttribute) { IsAttributedAsDontSerialize = true; } else if (attr is YAXSerializeAsAttribute) { Alias = StringUtils.RefineSingleElement((attr as YAXSerializeAsAttribute).SerializeAs); } else if (attr is YAXCollectionAttribute) { _collectionAttributeInstance = attr as YAXCollectionAttribute; } else if (attr is YAXDictionaryAttribute) { _dictionaryAttributeInstance = attr as YAXDictionaryAttribute; } else if (attr is YAXErrorIfMissedAttribute) { var temp = attr as YAXErrorIfMissedAttribute; DefaultValue = temp.DefaultValue; TreatErrorsAs = temp.TreatAs; } else if (attr is YAXFormatAttribute) { Format = (attr as YAXFormatAttribute).Format; } else if (attr is YAXNotCollectionAttribute) { // arrays are always treated as collections if (!ReflectionUtils.IsArray(MemberType)) { IsAttributedAsNotCollection = true; } } else if (attr is YAXCustomSerializerAttribute) { var serType = (attr as YAXCustomSerializerAttribute).CustomSerializerType; Type genTypeArg; var 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"); } if (genTypeArg != MemberType) { throw new YAXException("The generic argument of the class and the member type do not match"); } CustomSerializerType = serType; } else if (attr is YAXPreserveWhitespaceAttribute) { 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; Namespace = nsAttrib.Namespace; NamespacePrefix = nsAttrib.Prefix; } else if (attr is YAXTypeAttribute) { var yaxTypeAttr = attr as YAXTypeAttribute; var alias = yaxTypeAttr.Alias; if (alias != null) { alias = alias.Trim(); if (alias.Length == 0) { alias = null; } } if (_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, _memberInfo)); } if (alias != null && _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, _memberInfo)); } _possibleRealTypes.Add(yaxTypeAttr); } else if (attr is YAXCollectionItemTypeAttribute) { var yaxColletionItemTypeAttr = attr as YAXCollectionItemTypeAttribute; var alias = yaxColletionItemTypeAttr.Alias; if (alias != null) { alias = alias.Trim(); if (alias.Length == 0) { alias = null; } } if (_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, _memberInfo)); } if (alias != null && _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, _memberInfo)); } _possibleCollectionItemRealTypes.Add(yaxColletionItemTypeAttr); } else if (attr is YAXDontSerializeIfNullAttribute) { IsAttributedAsDontSerializeIfNull = true; } else if (attr is YAXElementOrder) { Order = (attr as YAXElementOrder).Order; } else { throw new Exception("Added new attribute type to the library but not yet processed!"); } }
/// <summary> /// Processes the specified attribute. /// </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 YAXSerializableTypeAttribute) { var theAttr = attr as YAXSerializableTypeAttribute; this.FieldsToSerialize = theAttr.FieldsToSerialize; if (theAttr.IsSerializationOptionSet()) { this.SerializationOption = theAttr.Options; this.m_isSerializationOptionSetByAttribute = true; } } else if (attr is YAXSerializeAsAttribute) { this.Alias = StringUtils.RefineSingleElement((attr as YAXSerializeAsAttribute).SerializeAs); } else if (attr is YAXNotCollectionAttribute) { if (!ReflectionUtils.IsArray(this.m_udtType)) { 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"); } if (genTypeArg != this.UnderlyingType) { throw new YAXException("The generic argument of the class and the type of the class do not match"); } this.CustomSerializerType = serType; } else if (attr is YAXPreserveWhitespaceAttribute) { this.PreservesWhitespace = true; } else if (attr is YAXNamespaceAttribute) { var nsAttrib = (attr as YAXNamespaceAttribute); this.Namespace = nsAttrib.Namespace; this.NamespacePrefix = nsAttrib.Prefix; } else if (attr is YAXCollectionAttribute) { this.m_collectionAttributeInstance = attr as YAXCollectionAttribute; } else if (attr is YAXDictionaryAttribute) { this.m_dictionaryAttributeInstance = attr as YAXDictionaryAttribute; } else { throw new Exception("Attribute not applicable to types!"); } }
/// <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) { IsAttributedAsSerializable = true; } else if (attr is YAXAttributeForClassAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null || (m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially)) { IsSerializedAsAttribute = true; SerializationLocation = "."; } } else if (attr is YAXValueForClassAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null || (m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially)) { IsSerializedAsValue = true; SerializationLocation = "."; } } else if (attr is YAXAttributeForAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null || (m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially)) { IsSerializedAsAttribute = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXAttributeForAttribute).Parent, out path, out alias); SerializationLocation = path; if (!String.IsNullOrEmpty(alias)) { Alias = StringUtils.RefineSingleElement(alias); } } } else if (attr is YAXElementForAttribute) { IsSerializedAsElement = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXElementForAttribute).Parent, out path, out alias); SerializationLocation = path; if (!String.IsNullOrEmpty(alias)) { Alias = StringUtils.RefineSingleElement(alias); } } else if (attr is YAXValueForAttribute) { // it is required that YAXCustomSerializerAttribute is processed earlier if (ReflectionUtils.IsBasicType(this.MemberType) || CustomSerializerType != null || (m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially)) { IsSerializedAsValue = true; string path, alias; StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXValueForAttribute).Parent, out path, out alias); SerializationLocation = path; if (!String.IsNullOrEmpty(alias)) { Alias = StringUtils.RefineSingleElement(alias); } } } else if (attr is YAXDontSerializeAttribute) { IsAttributedAsDontSerialize = true; } else if (attr is YAXSerializeAsAttribute) { Alias = StringUtils.RefineSingleElement((attr as YAXSerializeAsAttribute).SerializeAs); } else if (attr is YAXCollectionAttribute) { m_collectionAttributeInstance = attr as YAXCollectionAttribute; } else if (attr is YAXDictionaryAttribute) { m_dictionaryAttributeInstance = attr as YAXDictionaryAttribute; } else if (attr is YAXErrorIfMissedAttribute) { var temp = attr as YAXErrorIfMissedAttribute; DefaultValue = temp.DefaultValue; TreatErrorsAs = temp.TreatAs; } else if (attr is YAXFormatAttribute) { Format = (attr as YAXFormatAttribute).Format; } else if (attr is YAXNotCollectionAttribute) { // arrays are always treated as collections if (!ReflectionUtils.IsArray(MemberType)) { 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 { CustomSerializerType = serType; } } else if (attr is YAXPreserveWhitespaceAttribute) { 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); Namespace = nsAttrib.Namespace; NamespacePrefix = nsAttrib.Prefix; } else { throw new Exception("Added new attribute type to the library but not yet processed!"); } }