public EdiTypeDescriptor(Type clrType) { _ClrType = clrType; _Properties = new List <EdiPropertyDescriptor>(); // list inherited properties first; so SegmentGroups can inherit from there first Segment var clrProps = ClrType.GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(p => p.DeclaringType == clrType); var props = clrProps.Select(pi => new EdiPropertyDescriptor(pi)).Where(pi => pi.Attributes.Any()); // support for multiple value attributes on the same property. Bit hacky. foreach (var p in props) { var valueAttributes = p.Attributes.OfType <EdiValueAttribute>().ToArray(); if (valueAttributes.Length > 1) { foreach (var vAttr in valueAttributes) { _Properties.Add(new EdiPropertyDescriptor(p.Info, p.Attributes.Except(new[] { vAttr }))); } } else { _Properties.Add(p); } } _Attributes = new List <EdiAttribute>(); Attributes.AddRange(ClrType.GetTypeInfo().GetCustomAttributes <EdiAttribute>()); _SegmentGroupInfo = Attributes.OfType <EdiSegmentGroupAttribute>().SingleOrDefault(); _Path = Attributes.OfType <EdiPathAttribute>().FirstOrDefault()?.PathInternal ?? _SegmentGroupInfo?.StartInternal; }
public EdiPropertyDescriptor(PropertyInfo info, IEnumerable <EdiAttribute> attributes) { _Info = info; if (attributes == null) { attributes = info.GetCustomAttributes <EdiAttribute>() .Concat(info.PropertyType.GetTypeInfo().GetCustomAttributes <EdiAttribute>()); if (info.PropertyType.IsCollectionType()) { var itemType = default(Type); if (info.PropertyType.HasElementType) { itemType = info.PropertyType.GetElementType(); } else { itemType = Info.PropertyType.GetGenericArguments().First(); } attributes = attributes.Concat(itemType.GetTypeInfo().GetCustomAttributes <EdiAttribute>()); } } _Attributes = attributes.ToList(); _PathInfo = Attributes.OfType <EdiPathAttribute>().FirstOrDefault(); _ConditionInfo = Attributes.OfType <EdiConditionAttribute>().FirstOrDefault(); _ValueInfo = Attributes.OfType <EdiValueAttribute>().FirstOrDefault(); _SegmentGroupInfo = Attributes.OfType <EdiSegmentGroupAttribute>().FirstOrDefault(); if (_ValueInfo != null && _ValueInfo.Path != null && _PathInfo == null) { _PathInfo = new EdiPathAttribute(_ValueInfo.Path); } if (_SegmentGroupInfo != null && _SegmentGroupInfo.StartInternal.Segment != null && _PathInfo == null) { _PathInfo = new EdiPathAttribute(_SegmentGroupInfo.StartInternal.Segment); } }
/// <summary> /// Initializes a new instance of the <see cref="T:indice.Edi.Serialization.EdiTypeDescriptor"/> class. /// </summary> /// <param name="clrType">Clr type.</param> public EdiTypeDescriptor(Type clrType) { _ClrType = clrType; _Properties = new List <EdiPropertyDescriptor>(); var props = ClrType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(pi => new EdiPropertyDescriptor(pi)).Where(pi => pi.Attributes.Any()); // support for multiple value attributes on the same property. Bit hacky. foreach (var p in props) { var valueAttributes = p.Attributes.OfType <EdiValueAttribute>().ToArray(); if (valueAttributes.Length > 1) { foreach (var vAttr in valueAttributes) { _Properties.Add(new EdiPropertyDescriptor(p.Info, p.Attributes.Except(new[] { vAttr }))); } } else { _Properties.Add(p); } } _Attributes = new List <EdiAttribute>(); Attributes.AddRange(ClrType.GetTypeInfo().GetCustomAttributes <EdiAttribute>()); _SegmentGroupInfo = Attributes.OfType <EdiSegmentGroupAttribute>().SingleOrDefault(); }
public EdiPropertyDescriptor(PropertyInfo info, IEnumerable <EdiAttribute> attributes) { _Info = info; if (attributes == null) { attributes = info.GetCustomAttributes <EdiAttribute>() .Concat(info.PropertyType.GetTypeInfo().GetCustomAttributes <EdiAttribute>()); if (info.PropertyType.IsCollectionType()) { var itemType = default(Type); if (info.PropertyType.HasElementType) { itemType = info.PropertyType.GetElementType(); } else { itemType = Info.PropertyType.GetGenericArguments().First(); } attributes = attributes.Concat(itemType.GetTypeInfo().GetCustomAttributes <EdiAttribute>()); } } _Attributes = attributes.ToList(); var structureType = Attributes.InferStructure(); var pathInfo = Attributes.OfType <EdiPathAttribute>().FirstOrDefault(); var conditions = Attributes.OfType <EdiConditionAttribute>().ToArray(); _Conditions = conditions.Length > 0 ? conditions : null; _ValueInfo = Attributes.OfType <EdiValueAttribute>().FirstOrDefault(); _SegmentGroupInfo = Attributes.OfType <EdiSegmentGroupAttribute>().FirstOrDefault(); _AutoGenerationInfo = Attributes.OfType <EdiGeneratedAttribute>().FirstOrDefault(); if (_ValueInfo != null && _ValueInfo.Path != null && pathInfo == null) { pathInfo = new EdiPathAttribute(_ValueInfo.Path); } if (_SegmentGroupInfo != null && _SegmentGroupInfo.StartInternal.Segment != null && pathInfo == null) { pathInfo = new EdiPathAttribute(_SegmentGroupInfo.StartInternal.Segment); } if (pathInfo != null && structureType == EdiStructureType.None && pathInfo.PathInternal.Segment.IsWildcard) { var parentPathInfo = info.DeclaringType.GetTypeInfo().GetCustomAttributes <EdiPathAttribute>().FirstOrDefault(); if (parentPathInfo != null) { pathInfo = new EdiPathAttribute(new EdiPath(new EdiPathFragment(parentPathInfo.Segment), pathInfo.PathInternal.Element, pathInfo.PathInternal.Component)); } } if (pathInfo == null && structureType == EdiStructureType.Element) { pathInfo = info.DeclaringType.GetTypeInfo().GetCustomAttributes <EdiPathAttribute>().FirstOrDefault(); if (pathInfo != null) { pathInfo = new EdiPathAttribute(new EdiPath(new EdiPathFragment(pathInfo.Segment), new EdiPathFragment("*"), new EdiPathFragment(""))); } } _PathInfo = pathInfo; _ConditionStackMode = conditions.Length > 0 && Attributes.OfType <EdiAnyAttribute>().Any() ? EdiConditionStackMode.Any : EdiConditionStackMode.All; }
public EdiTypeDescriptor(Type clrType) { _ClrType = clrType; _Properties = new List<EdiPropertyDescriptor>(); var props = ClrType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(pi => new EdiPropertyDescriptor(pi)).Where(pi => pi.Attributes.Any()); // support for multiple value attributes on the same property. Bit hacky. foreach (var p in props) { var valueAttributes = p.Attributes.OfType<EdiValueAttribute>().ToArray(); if (valueAttributes.Length > 1) { foreach (var vAttr in valueAttributes) { _Properties.Add(new EdiPropertyDescriptor(p.Info, p.Attributes.Except(new[] { vAttr }))); } } else { _Properties.Add(p); } } _Attributes = new List<EdiAttribute>(); Attributes.AddRange(ClrType.GetTypeInfo().GetCustomAttributes<EdiAttribute>()); _SegmentGroupInfo = Attributes.OfType<EdiSegmentGroupAttribute>().SingleOrDefault(); }
public EdiPropertyDescriptor(PropertyInfo info, IEnumerable<EdiAttribute> attributes) { _Info = info; if (attributes == null) { attributes = info.GetCustomAttributes<EdiAttribute>() .Concat(info.PropertyType.GetTypeInfo().GetCustomAttributes<EdiAttribute>()); if (info.PropertyType.IsCollectionType()) { var itemType = default(Type); if (info.PropertyType.HasElementType) { itemType = info.PropertyType.GetElementType(); } else { itemType = Info.PropertyType.GetGenericArguments().First(); } attributes = attributes.Concat(itemType.GetTypeInfo().GetCustomAttributes<EdiAttribute>()); } } _Attributes = attributes.ToList(); _PathInfo = Attributes.OfType<EdiPathAttribute>().FirstOrDefault(); _ConditionInfo = Attributes.OfType<EdiConditionAttribute>().FirstOrDefault(); _ValueInfo = Attributes.OfType<EdiValueAttribute>().FirstOrDefault(); _SegmentGroupInfo = Attributes.OfType<EdiSegmentGroupAttribute>().FirstOrDefault(); if (_ValueInfo != null && _ValueInfo.Path != null && _PathInfo == null) { _PathInfo = new EdiPathAttribute(_ValueInfo.Path); } if (_SegmentGroupInfo != null && _SegmentGroupInfo.StartInternal.Segment != null && _PathInfo == null) { _PathInfo = new EdiPathAttribute(_SegmentGroupInfo.StartInternal.Segment); } }