public static List <ExportProperty> ReadMetadata(Type exportType) { // Prepare output var exportProperties = new List <ExportProperty>(); // Get properties and prepare exportType.GetProperties().ToList().ForEach(property => { // Create with defaults var exportProperty = new ExportProperty { Header = property.Name, Property = new ExportPropertyInfo(property) }; // Apply settings from attribute if given var exportAttribute = property.GetCustomAttributes(typeof(ExportToExcelAttribute), false).FirstOrDefault() as ExportToExcelAttribute; if (exportAttribute != null && !exportAttribute.Ignore) { ApplyExportAttributeToProperty(exportAttribute, exportProperty); } // Add to collection if not ignored if (exportAttribute == null || !exportAttribute.Ignore) { exportProperties.Add(exportProperty); } }); return(exportProperties); }
private static void ApplyExportAttributeToProperty(ExportToExcelAttribute attribute, ExportProperty property) { if (!string.IsNullOrEmpty(attribute.Header)) { property.Header = attribute.Header; } if (!string.IsNullOrEmpty(attribute.StringFormat)) { property.Format = attribute.StringFormat; } if (!string.IsNullOrEmpty(attribute.DisplayMemberPath)) { var parts = attribute.DisplayMemberPath.Split('.'); var currentParentProperty = property.Property; for (var partIndex = 0; partIndex < parts.Length; partIndex++) { var part = parts[partIndex]; var partProperty = currentParentProperty.PropertyInfo.PropertyType.GetProperty(part); var partExportProperty = new ExportPropertyInfo(partProperty); currentParentProperty.InnerProperty = partExportProperty; currentParentProperty = partExportProperty; } } }