private async Task <List <ViewerProperty> > FlatProperties(Dictionary <string, object> customAttributes, string projectId) { var properties = new List <ViewerProperty>(); var attrDefsResponse = await GetCustomAttributeDefsAsync(projectId, null, 100); var attrDefs = JsonConvert.DeserializeObject <PaginatedAssetCustomAttributes>(attrDefsResponse.Content); var attrDefMapping = attrDefs.Results.ToDictionary(d => d.Name, d => d); Func <string, string> getAttrDefName = (name) => (!string.IsNullOrWhiteSpace(name) && attrDefMapping.ContainsKey(name)) ? attrDefMapping[name].DisplayName : string.Empty; foreach (var pair in customAttributes) { var attrName = pair.Key; var attrVal = pair.Value.ToString(); var attrType = this.ConvertAttributeType(attrVal.GetType()); //!-- Todo: get custom attribute name from Assets API var property = new ViewerProperty { AttributeName = attrName, DisplayCategory = "Custom", DisplayName = getAttrDefName(attrName), DisplayValue = attrVal, Hidden = 0, Precision = 0, Type = attrType, Units = null }; properties.Add(property); } return(properties); }
private async Task <List <ViewerProperty> > FlatProperties(Asset asset, string projectId) { var properties = new List <ViewerProperty>(); foreach (System.Reflection.PropertyInfo pi in asset.GetType().GetProperties()) { var attrName = pi.Name; if (attrName == "CustomAttributes") { var flattenCustomAttrs = await this.FlatProperties(((Dictionary <string, object>)pi.GetValue(asset, null)), projectId); properties.AddRange(flattenCustomAttrs); continue; } var isHidden = (attrName.EndsWith("Id") || attrName.EndsWith("By")) ? 1 : 0; var attrType = this.ConvertAttributeType(pi.PropertyType); var attrVal = pi.GetValue(asset, null)?.ToString(); if (pi.PropertyType == typeof(DateTime?)) { attrVal = ((DateTime?)pi.GetValue(asset, null))?.ToUniversalTime() .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"); } var property = new ViewerProperty { AttributeName = attrName, DisplayCategory = "Built In", DisplayName = Regex.Replace(attrName, "(\\B[A-Z])", " $1"), DisplayValue = attrVal, Hidden = isHidden, Precision = 0, Type = attrType, Units = null }; properties.Add(property); } return(properties); }