private object AssignValueToCustomType(CustomModelWithType customModelInfo, DataWrapperContext wrapperContext) { if (customModelInfo == null) { return(null); } var modelInstance = Activator.CreateInstance(customModelInfo.EmitType); foreach (var customerProperty in customModelInfo.ModelConfig.GetAllConfigProperties()) { var propertyValue = customerProperty.Value(wrapperContext); customModelInfo.EmitType.GetProperty(customerProperty.Key)?.SetValue(modelInstance, propertyValue); } return(modelInstance); }
/// <summary> /// Create customModel instance,and given value by config. /// </summary> /// <param name="wrapperContext"><see cref="DataWrapperContext"/></param> /// <returns>customModel instance.</returns> protected virtual object CreateCustomModel(DataWrapperContext wrapperContext) { var currentHttpCode = wrapperContext.HttpContext.Response.StatusCode; if (_cacheCustomerType.TryGetValue(currentHttpCode, out var resultConfigInfo)) { return(AssignValueToCustomType(resultConfigInfo, wrapperContext)); } var customModels = wrapperContext.WrapperOptions.CustomModelConfig; CustomWrapperModel optimizationCustomModel = null; foreach (var customModel in customModels) { if (customModel.Key.IsInRange(currentHttpCode)) { optimizationCustomModel = customModel.Value; break; } } if (optimizationCustomModel == null) { //If it is not empty, it means that there is no configuration, //then when the status code is encountered in the future, the source data will be returned _cacheCustomerType.TryAdd(currentHttpCode, null); return(null); } var emitType = CreateCustomEmitType(optimizationCustomModel); var customModelInfo = new CustomModelWithType(optimizationCustomModel, emitType); _cacheCustomerType.TryAdd(currentHttpCode, customModelInfo); return(AssignValueToCustomType(customModelInfo, wrapperContext)); }