// // // // special case for enums // if (targetType.IsEnum) { // // we could be going from an int -> enum so specifically let // // the Enum object take care of this conversion // if (value != null) { // value = Enum.ToObject(targetType, value); // } // } // else { // // returns an System.Object with the specified System.Type and whose value is // // equivalent to the specified object. // value = Convert.ChangeType(value, targetType); // } // set the value of the property // propertyInfo.SetValue(target, value, null); // } private async Task processHasMany(SuperModel model, Type modelType, PropertyInfo propertyInfo, HasManyAttribute attribute) { var propertyType = CascadeTypeUtils.DeNullType(propertyInfo.PropertyType); var isEnumerable = (propertyType?.Implements <IEnumerable>() ?? false) && propertyType != typeof(string); var foreignType = isEnumerable ? CascadeTypeUtils.InnerType(propertyType !) : null; foreignType = foreignType != null?CascadeTypeUtils.DeNullType(foreignType) : null; if (foreignType == null) { throw new ArgumentException("Unable to get foreign model type. Property should be of type ImmutableArray<ChildModel>"); } // var typeLayers = GetTypeLayers(propertyType); // var nonNullableType = DeNullType(propertyType); // // typeLayers.FirstOrDefault(t => t.Name != "Nullable`1"); // // var foreignType = isEnumerable ? propertyType.GetGenericArguments().FirstOrDefault() : null; object modelId = CascadeTypeUtils.GetCascadeId(model); var key = $"HasMany__{foreignType.Name}__{attribute.ForeignIdProperty}__{modelId}"; var requestOp = new RequestOp( NowMs, foreignType, RequestVerb.Query, null, null, 0, new Dictionary <string, object>() { [attribute.ForeignIdProperty] = modelId }, key ); var opResponse = await ProcessRequest(requestOp); CascadeTypeUtils.SetModelCollectionProperty(model, propertyInfo, opResponse.Results); //propertyInfo.SetValue(model,opResponse.Results); }
private async Task processBelongsTo(object model, Type modelType, PropertyInfo propertyInfo, BelongsToAttribute attribute) { var foreignModelType = CascadeTypeUtils.DeNullType(propertyInfo.PropertyType); var idProperty = modelType.GetProperty(attribute.IdProperty); var id = idProperty.GetValue(model); if (id == null) { return; } var requestOp = new RequestOp( NowMs, foreignModelType, RequestVerb.Get, id, freshnessSeconds: Config.DefaultFreshnessSeconds ); var opResponse = await ProcessRequest(requestOp); SetModelProperty(model, propertyInfo, opResponse.Result); }