/// <summary> /// Decompose raw value into a multivalue reference list DTOs /// </summary> /// <typeparam name="T"></typeparam> /// <param name="rawValue"></param> /// <returns></returns> public static List <ReferenceListItemValueDto> DecomposeMultiReferenceListValue <T>(T rawValue) where T : struct, IConvertible { var result = new List <ReferenceListItemValueDto>(); if (rawValue.ToString() == "0") { return(result); } var flag = Enum.Parse(typeof(T), rawValue.ToString()) as Enum; foreach (var r in (long[])Enum.GetValues(typeof(T))) { if ((Convert.ToInt64(flag) & r) == r) { var nameValue = new ReferenceListItemValueDto() { Item = Enum.GetName(typeof(T), r), ItemValue = r }; result.Add(nameValue); } } return(result); }
private static object GetRefListItemValue(ReferenceListItemValueDto dto, Type srcPropType, Type dstPropType) { if (dto?.ItemValue == null) { return(null); } if (srcPropType.IsEnum) { return(Enum.ToObject(srcPropType, dto.ItemValue)); } var dstType = dstPropType.GetUnderlyingTypeIfNullable(); if (dstType.IsEnum) { var enumUnderlayingType = dstType.GetEnumUnderlyingType(); var numericValue = System.Convert.ChangeType(dto.ItemValue.Value, enumUnderlayingType); return(Enum.ToObject(dstType, numericValue)); } return(System.Convert.ChangeType(dto.ItemValue.Value, dstType)); }
private async Task <object> GetPropertyValueAsync <TRow, TId>(TRow entity, string propertyName, bool isExport, string defaultValue = "") where TRow : class, IEntity <TId> { try { PropertyInfo propInfo = null; object parentEntity = null; var val = IsDynamic ? await DynamicPropertyManager.Value.GetEntityPropertyAsync <TRow, TId>(entity, propertyName) : ReflectionHelper.GetPropertyValue(entity, propertyName, out parentEntity, out propInfo); if (val == null) { return(defaultValue); } var valueType = val.GetType(); if (valueType.IsEnum) { var itemValue = Convert.ToInt64(val); var enumType = valueType; if (enumType.IsNullable()) { enumType = Nullable.GetUnderlyingType(enumType); } return(ReflectionHelper.GetEnumDescription(enumType, itemValue)); } if (valueType.IsEntityType()) { var displayProperty = valueType.GetEntityConfiguration().DisplayNamePropertyInfo; var displayText = displayProperty != null ? displayProperty.GetValue(val)?.ToString() : val.ToString(); if (DataTableConfig != null && !DataTableConfig.UseDtos || isExport) { return(displayText); } var dto = new EntityWithDisplayNameDto <string>(val.GetId().ToString(), displayText); return(dto); } var propConfig = parentEntity != null && propInfo != null ? parentEntity.GetType().GetEntityConfiguration()[propInfo.Name] : null; if (propConfig != null) { if (propConfig.GeneralType == Configuration.Runtime.GeneralDataType.ReferenceList) { var refListHelper = StaticContext.IocManager.Resolve <IReferenceListHelper>(); var itemValue = Convert.ToInt64(val); var displayText = refListHelper.GetItemDisplayText(propConfig.ReferenceListNamespace, propConfig.ReferenceListName, itemValue); if (DataTableConfig != null && !DataTableConfig.UseDtos || isExport) { return(displayText); } var dto = new ReferenceListItemValueDto { Item = displayText, ItemValue = itemValue }; return(dto); } else { return(EntityExtensions.GetPrimitiveTypePropertyDisplayText(val, propInfo, defaultValue)); } } return(val); } catch (Exception ex) { throw new Exception( $"An error occured whilst trying to retrieve DisplayText of property '{propertyName}' on type of '{entity.GetType().FullName}'.", ex); } }