public void GetEnumerableItemType_IEnumerableOfSimpleTestItem_ReturnsTestItemType() { var testItemList = typeof(IEnumerable <SimpleTestItem>); var itemType = FormatterUtils.GetEnumerableItemType(testItemList); Assert.IsNotNull(itemType); Assert.AreEqual(typeof(SimpleTestItem), itemType); }
public void GetEnumerableItemType_ArrayOfSimpleTestItem_ReturnsTestItemType() { var testItemArray = typeof(SimpleTestItem[]); var itemType = FormatterUtils.GetEnumerableItemType(testItemArray); Assert.IsNotNull(itemType); Assert.AreEqual(typeof(SimpleTestItem), itemType); }
public void GetEnumerableItemType_ArrayOfSimpleTestItem_ReturnsTestItemType() { var testItemArray = typeof(SimpleTestItem[]); var itemType = FormatterUtils.GetEnumerableItemType(testItemArray); itemType.Should().NotBeNull(); itemType.Should().Be(typeof(SimpleTestItem)); }
public void GetEnumerableItemType_IEnumerableOfSimpleTestItem_ReturnsTestItemType() { var testItemList = typeof(IEnumerable <SimpleTestItem>); var itemType = FormatterUtils.GetEnumerableItemType(testItemList); itemType.Should().NotBeNull(); itemType.Should().Be(typeof(SimpleTestItem)); }
public void GetEnumerableItemType_ListOfAnonymousObject_ReturnsTestItemType() { var anonymous = new { prop1 = "value1", prop2 = "value2" }; var anonymousList = new[] { anonymous }.ToList(); var itemType = FormatterUtils.GetEnumerableItemType(anonymousList.GetType()); Assert.IsNotNull(itemType); Assert.AreEqual(anonymous.GetType(), itemType); }
public void GetEnumerableItemType_ListOfAnonymousObject_ReturnsTestItemType() { var anonymous = new { prop1 = "value1", prop2 = "value2" }; var anonymousList = new[] { anonymous }.ToList(); var itemType = FormatterUtils.GetEnumerableItemType(anonymousList.GetType()); itemType.Should().NotBeNull(); itemType.Should().Be(anonymous.GetType()); }
public ExcelSheetInfoCollection GetExcelSheetInfo(Type itemType, object data) { if (itemType.Name.StartsWith("Dictionary")) { return(null); } var sheets = FormatterUtils.GetMemberNames(itemType); var properties = GetSerialisablePropertyInfo(itemType); var sheetCollection = new ExcelSheetInfoCollection(); foreach (var sheet in sheets) { var prop = properties.FirstOrDefault(p => p.Name == sheet); ExcelSheetAttribute attr = FormatterUtils.GetAttribute <ExcelSheetAttribute>(prop); if (prop == null || attr == null) { continue; } var sheetInfo = new ExcelSheetInfo() { SheetType = prop.PropertyType, SheetName = sheet, ExcelSheetAttribute = attr, PropertyName = prop.Name, SheetObject = FormatterUtils.GetFieldOrPropertyValue(data, prop.Name) }; if (prop.PropertyType.Name.StartsWith("List") && sheetInfo.SheetObject != null) { sheetInfo.SheetType = FormatterUtils.GetEnumerableItemType(sheetInfo.SheetObject.GetType()); } sheetCollection.Add(sheetInfo); } return(sheetCollection); }
/// <summary> /// Get the <c>ExcelColumnInfo</c> for all members of a class. /// </summary> /// <param name="itemType">Type of item being serialised.</param> /// <param name="data">The collection of values being serialised. (Not used, provided for use by derived /// types.)</param> public virtual ExcelColumnInfoCollection GetExcelColumnInfo(Type itemType, object data, string namePrefix = "", bool isComplexColumn = false) { var fieldInfo = new ExcelColumnInfoCollection(); if (itemType.Name.StartsWith("Dictionary")) { var prefix = namePrefix + "_Dict_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, new ExcelColumnAttribute(), null)); return(fieldInfo); } var fields = GetSerialisableMemberNames(itemType, data); var properties = GetSerialisablePropertyInfo(itemType, data); // Instantiate field names and fieldInfo lists with serialisable members. foreach (var field in fields) { var prop = properties.FirstOrDefault(p => p.Name == field); if (prop == null) { continue; } Type propertyType = prop.PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = propertyType.GetGenericArguments()[0]; } ExcelColumnAttribute attribute = FormatterUtils.GetAttribute <ExcelColumnAttribute>(prop); if (attribute != null) { string prefix = string.IsNullOrEmpty(namePrefix) == false ? $"{namePrefix}:{prop.Name}" : prop.Name; if (propertyType.Name.StartsWith("List")) { Type typeOfList = FormatterUtils.GetEnumerableItemType(propertyType); //if (FormatterUtils.IsSimpleType(typeOfList)) //{ // fieldInfo.Add(new ExcelColumnInfo(prefix, typeOfList, attribute, null)); //} //else if (typeOfList.FullName.EndsWith("CustomFieldModel") || typeOfList.Name.StartsWith("OverrideProperty")) { prefix += "_CustomField_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null)); } else { prefix += "_List_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null)); //ExcelColumnInfoCollection columnCollection = GetExcelColumnInfo(typeOfList, null, prefix, true); //foreach (var subcolumn in columnCollection) // fieldInfo.Add(subcolumn); } } else if (propertyType.Name.EndsWith("CustomFieldModel") || propertyType.Name.StartsWith("OverrideProperty")) { prefix += "_CustomField_Single_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null)); } else if (propertyType.Name.StartsWith("Dictionary")) { prefix += "_Dict_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null)); } else if (!FormatterUtils.IsSimpleType(propertyType)) { ExcelColumnInfoCollection columnCollection = GetExcelColumnInfo(propertyType, null, prefix, true); foreach (var subcolumn in columnCollection) { fieldInfo.Add(subcolumn); } } else { string propertyName = isComplexColumn ? $"{namePrefix}:{field}" : field; string displayName = propertyName; if (attribute.DoNotUsePropertyName) { attribute.Header = namePrefix; } bool columnAlreadyadded = fieldInfo.Any(a => a.PropertyName == propertyName); if (!columnAlreadyadded) { if (FormatterUtils.IsExcelSupportedType(propertyType)) { fieldInfo.Add(new ExcelColumnInfo(propertyName, propertyType, attribute, null)); } else { fieldInfo.Add(new ExcelColumnInfo(propertyName, typeof(string), attribute, null)); } } } } } PopulateFieldInfoFromMetadata(fieldInfo, itemType, data); return(fieldInfo); }