/// <summary> /// Bind object property type of object /// </summary> /// <param name="type">Type objec</param> /// <param name="modelToBind">Instance object</param> /// <param name="relationships">List object</param> private void BindRelationships(Type type, object modelToBind, IEnumerable <JToken> relationships) { foreach (JProperty attr in relationships) { PropertyInfo relationship = AttributeHandling.GetProperty(type, attr.Name); if (relationship == null) { throw new InvalidPropertyException(type, attr.Name, true); } Type typeRelationship = AttributeHandling.GetTypeProperty(relationship); object tempObj = Activator.CreateInstance(typeRelationship); if (Utils.IsEnum(tempObj)) { if (this.GetTypeJProperty(attr.First()) != "Array") { throw new InvalidPropertyException("json", attr.Name, "Array"); } //--------Cas particulier------// //Type spécifié Type sendedType = typeRelationship.GetGenericArguments()[0]; //Type attendu Type waitingType = relationship.PropertyType.GetGenericArguments()[0]; //Instanciation particulière lorsque le type attendu dans le BO est une List<Interface> if (waitingType.GetTypeInfo().IsInterface) { tempObj = Activator.CreateInstance(typeof(List <>).MakeGenericType(waitingType)); } //---------------------------// this.BindList(sendedType, tempObj, attr.First()); } else { this.BindModel(typeRelationship, tempObj, attr.First()); } relationship.SetValue(modelToBind, tempObj); } }
/// <summary> /// Bind object property with simple type (string, bool, date, int) /// </summary> /// <param name="type">Type object</param> /// <param name="modelToBind">Instance object</param> /// <param name="attributes">List properties values</param> private void BindAttributes(Type type, object modelToBind, IEnumerable <JToken> attributes) { // Set known values foreach (JProperty attr in attributes) { PropertyInfo property = AttributeHandling.GetProperty(type, attr.Name); if (property == null) { this._error.Create(Constants.ERROR_STATUT_JSONAPI, $"Invalid Property {attr.Name}", $"{ AttributeHandling.GetLabelProperty(type)} has not property {attr.Name}", ""); } else if (property.CanWrite) { try { Type typeAttribute = AttributeHandling.GetTypeProperty(property); object valueFormat = this.ReadJsonValue(typeAttribute, attr); if (this.ValidateData(property, attr.Name, valueFormat)) { valueFormat = this.ResolveData(property, valueFormat); property.SetValue(modelToBind, valueFormat); } } catch (FormatException ex) { this._error.Create(Constants.ERROR_STATUT_JSONAPI, $"Error Format on attribute {attr.Name}", ex.Message, ""); } } } // Set empty properties with resolvers foreach (PropertyInfo property in type.GetProperties().Where(p => p.CanWrite && p.GetValue(modelToBind) == null)) { object valueResolved = this.ResolveData(property, null); if (valueResolved != null) { property.SetValue(modelToBind, valueResolved); } } }