/// <summary>
/// 指定是否保存到文件中
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class FileSaveAttribute : Attribute
{
public bool Save { get; set; }
public FileSaveAttribute(bool isSave)
{
Save = isSave;
}
}
/// <summary>
/// 指定属性的格式化格式
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class FormatAttribute : Attribute
{
public string Format { get; set; }
public FormatAttribute(string format)
{
Format = format;
}
}
public class FileSave<T>
{
public void CsvSave(string path, List<T> list, string title = null)
{
StringBuilder sb = new StringBuilder();
if (title != null)
{
sb.AppendLine(title);
}
Type tInfo = typeof(T);
List<object> listHeader = new List<object>();
Dictionary<string, object> dictData = new Dictionary<string, object>();
foreach (PropertyInfo p in tInfo.GetProperties())
{
string key = p.Name;
if (tInfo.GetAttributes<FileSaveAttribute>(key, out object isSave))
{
if (!(bool)isSave)
{
continue;
}
}
//不存在默认为保存
tInfo.GetAttributes<DisplayNameAttribute>(key, out object displayName);
listHeader.Add(displayName ?? key);
dictData[key] = null;
}
sb.AppendLine(string.Join(",", listHeader));
foreach (var model in list)
{
foreach (PropertyInfo p in tInfo.GetProperties())
{
string key = p.Name;
if (dictData.ContainsKey(key))
{
dictData[key] = p.GetValue(model, null);
}
}
sb.AppendLine(string.Join(",", dictData.Values));
}
File.WriteAllText(path, sb.ToString(), Encoding.Default);
}
}
public static class ExtentionMethod
{
/// <summary>
/// 获取该类型中的属性有没有对应的特性值
/// </summary>
/// <typeparam name="T">特性类型</typeparam>
/// <param name="type">查找的class类型</param>
/// <param name="propertyName">class中的属性名称</param>
/// <param name="value">out:特性值</param>
/// <returns>是否存在该特性</returns>
public static bool GetAttributes<T>(this Type type, string propertyName, out object value) where T : Attribute
{
value = default;
Type attributeType = typeof(T);
PropertyInfo propertyInfo = type.GetProperties().FirstOrDefault(p => p.Name == propertyName);
if (propertyInfo != null)
{
Attribute attr = Attribute.GetCustomAttribute(propertyInfo, attributeType);
if (attr != null)
{
string attrPropertyName = string.Empty;
switch (attributeType.Name)
{
case "CategoryAttribute": attrPropertyName = "Category"; break;
case "DescriptionAttribute": attrPropertyName = "Description"; break;
case "DisplayNameAttribute": attrPropertyName = "DisplayName"; break;
case "ReadOnlyAttribute": attrPropertyName = "IsReadOnly"; break;
case "BrowsableAttribute": attrPropertyName = "Browsable"; break;
case "DefaultValueAttribute": attrPropertyName = "Value"; break;
case "FileSaveAttribute": attrPropertyName = "Save"; break;
case "FormatAttribute": attrPropertyName = "Format"; break;
default:
return false;
}
PropertyDescriptorCollection attrPropertys = TypeDescriptor.GetProperties(type);
var fs = attributeType.GetProperties();
PropertyInfo attrPropertyInfo = attributeType.GetProperty(attrPropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance);
value = attrPropertyInfo.GetValue(attrPropertys[propertyInfo.Name].Attributes[attributeType]);
return true;
}
}
return false;
}
}
C#csv文件保存(通用版)
于 2021-08-06 16:57:10 首次发布
这个博客介绍了一个名为`FileSave`的类,它包含一个`CsvSave`方法用于将泛型列表数据保存为CSV文件。方法首先检查属性上的`FileSaveAttribute`和`DisplayNameAttribute`特性来决定哪些属性应被保存,并根据`FormatAttribute`进行格式化。博客还提供了一个扩展方法`GetAttributes<T>`,用于获取类型中特定属性的特性值。
摘要由CSDN通过智能技术生成