private static void WriteResourceProperties(Representation representation, XmlWriter writer) { // Only simple type and nested ApiResource type will be handled : for any other type, exception will be thrown // including List<ApiResource> as representation of List would require properties rel, href and linkname // To overcome the issue, use "RepresentationList<T>" foreach (var property in representation.GetType().GetPublicInstanceProperties()) { if (property.IsValidBasicType()) { var propertyString = GetPropertyString(property, representation); if (propertyString != null) { writer.WriteElementString(property.Name, propertyString); } } else if (typeof(Representation).IsAssignableFrom(property.PropertyType) && property.GetIndexParameters().Length == 0) { var halResource = property.GetValue(representation, null); WriteHalResource((Representation)halResource, writer, property.Name); } else if (typeof(IEnumerable <Representation>).IsAssignableFrom(property.PropertyType)) { if (property.GetValue(representation, null) is IEnumerable <Representation> halResourceList) { foreach (var item in halResourceList) { WriteHalResource(item, writer); } } } } }
static void WriteResourceProperties(Representation representation, XmlWriter writer) { // Only simple type and nested ApiResource type will be handled : for any other type, exception will be thrown // including List<ApiResource> as representation of List would require properties rel, href and linkname // To overcome the issue, use "RepresentationList<T>" foreach (var property in representation.GetType().GetPublicInstanceProperties()) { if (property.IsValidBasicType()) { var propertyString = GetPropertyString(property, representation); if (propertyString != null) { writer.WriteElementString(property.Name, propertyString); } } else if (typeof (Representation).IsAssignableFrom(property.PropertyType) && property.GetIndexParameters().Length == 0) { var halResource = property.GetValue(representation, null); WriteHalResource((Representation) halResource, writer, property.Name); } else if (typeof (IEnumerable<Representation>).IsAssignableFrom(property.PropertyType)) { var halResourceList = property.GetValue(representation, null) as IEnumerable<Representation>; if (halResourceList != null) foreach (var item in halResourceList) WriteHalResource(item, writer); } } }
private static void WriteResourceProperties(Representation value, XmlWriter writer) { var resourceType = value.GetType(); var allProps = resourceType.GetPublicInstanceProperties(); if (resourceType.IsResourceTyped()) { var modelProp = allProps.First(p => p.IsModelProperty()); var modelValue = modelProp.GetValue(value, null); var modelType = modelProp.PropertyType; WriteResourceProperties(modelValue, writer, modelType.GetPublicInstanceProperties()); var propsOthers = allProps.Where(p => !p.IsModelProperty()); WriteResourceProperties(value, writer, propsOthers); } else { WriteResourceProperties(value, writer, allProps); } }