protected virtual object GetFieldOrPropertyValue(object rowObject, string name) { var rowValue = FormatterUtils.GetFieldOrPropertyValue(rowObject, name); if (rowValue is DateTimeOffset) { return(FormatterUtils.ConvertFromDateTimeOffset((DateTimeOffset)rowValue)); } else if (rowObject is Dictionary <string, object> ) { return((rowObject as Dictionary <string, object>)[name]); } else if (FormatterUtils.IsExcelSupportedType(rowValue)) { return(rowValue); } else if ((rowValue is IEnumerable <object>)) { return(rowValue); } // return string.Join(",", rowValue as IEnumerable<object>); return(rowValue == null || DBNull.Value.Equals(rowValue) ? string.Empty : rowValue.ToString()); }
public void GetFieldOrPropertyValueT_AnonymousObject_ReturnsPropertyValues() { var obj = new { prop1 = "test", prop2 = 2.0, prop3 = DateTime.Today }; Assert.AreEqual(obj.prop1, FormatterUtils.GetFieldOrPropertyValue <string>(obj, "prop1")); Assert.AreEqual(obj.prop2, FormatterUtils.GetFieldOrPropertyValue <double>(obj, "prop2")); Assert.AreEqual(obj.prop3, FormatterUtils.GetFieldOrPropertyValue <DateTime>(obj, "prop3")); }
public void GetFieldOrPropertyValueT_AnonymousObject_ReturnsPropertyValues() { var obj = new { prop1 = "test", prop2 = 2.0, prop3 = DateTime.Today }; FormatterUtils.GetFieldOrPropertyValue <string>(obj, "prop1").Should().Be(obj.prop1); FormatterUtils.GetFieldOrPropertyValue <double>(obj, "prop2").Should().Be(obj.prop2); FormatterUtils.GetFieldOrPropertyValue <DateTime>(obj, "prop3").Should().Be(obj.prop3); }
public void GetFieldOrPropertyValueT_ComplexTestItem_ReturnsPropertyValues() { var obj = new ComplexTestItem() { Value1 = "Value 1", Value2 = DateTime.Today, Value3 = true, Value4 = 100.1, Value5 = TestEnum.Second, Value6 = "Value 6" }; Assert.AreEqual(obj.Value1, FormatterUtils.GetFieldOrPropertyValue <string>(obj, "Value1")); Assert.AreEqual(obj.Value2, FormatterUtils.GetFieldOrPropertyValue <DateTime>(obj, "Value2")); Assert.AreEqual(obj.Value3, FormatterUtils.GetFieldOrPropertyValue <bool>(obj, "Value3")); Assert.AreEqual(obj.Value4, FormatterUtils.GetFieldOrPropertyValue <double>(obj, "Value4")); Assert.AreEqual(obj.Value5, FormatterUtils.GetFieldOrPropertyValue <TestEnum>(obj, "Value5")); Assert.AreEqual(obj.Value6, FormatterUtils.GetFieldOrPropertyValue <string>(obj, "Value6")); }
public void GetFieldOrPropertyValueT_ComplexTestItem_ReturnsPropertyValues() { var obj = new ComplexTestItem { Value1 = "Value 1", Value2 = DateTime.Today, Value3 = true, Value4 = 100.1, Value5 = TestEnum.Second, Value6 = "Value 6" }; FormatterUtils.GetFieldOrPropertyValue <string>(obj, "Value1").Should().Be(obj.Value1); FormatterUtils.GetFieldOrPropertyValue <DateTime>(obj, "Value2").Should().Be(obj.Value2); FormatterUtils.GetFieldOrPropertyValue <bool>(obj, "Value3").Should().Be(obj.Value3); FormatterUtils.GetFieldOrPropertyValue <double>(obj, "Value4").Should().Be(obj.Value4); FormatterUtils.GetFieldOrPropertyValue <TestEnum>(obj, "Value5").Should().Be(obj.Value5); FormatterUtils.GetFieldOrPropertyValue <string>(obj, "Value6").Should().Be(obj.Value6); }
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); }