/// <summary> /// 获取枚举描述信息对象 /// </summary> /// <param name="type">枚举项</param> /// <returns></returns> private EnumObject getEnumObject(Type type) { EnumObject enumObject = new EnumObject(); var entitys = GetEntitys(type); enumObject.EnumObjectName = type.Name; enumObject.EntityAndField = entitys; //获取枚举类 Array enumValue = type.GetFields(BindingFlags.Static | BindingFlags.Public); List <EnumInfo> enumInfos = new List <EnumInfo>(); for (var i = 0; i < enumValue.Length; i++) { if (((FieldInfo[])enumValue)[i].FieldType != typeof(int)) { continue; } EnumInfo enumInfo = new EnumInfo(); enumInfo.EnumName = ((FieldInfo[])enumValue)[i].Name; enumInfo.EnumValue = Convert.ToInt32(((FieldInfo[])enumValue)[i].GetValue(null)); MultiLanguageAttribute multi = ((FieldInfo[])enumValue)[i].GetCustomAttribute <MultiLanguageAttribute>(); enumInfo.Description = multi == null ? "" : Resource(multi.ResourceKey); enumInfos.Add(enumInfo); } enumObject.EnumInfos = enumInfos; return(enumObject); }
/// <summary> /// 枚举描述对象转字符串 /// </summary> /// <param name="enumObject">枚举描述对象</param> /// <returns></returns> private string getEnumObjectString(EnumObject enumObject) { StringBuilder sb = new StringBuilder(); string enumMainInfo = $"枚举名称:{enumObject.EnumObjectName}\t实体和字段:{string.Join(", ", enumObject.EntityAndField)}"; sb.AppendLine(enumMainInfo); sb.AppendLine("EnumName \tEnumValue\tDescription"); string enumInfoTemp = "{0}\t{1}\t{2}"; // 创建新增行 for (var i = 0; i < enumObject.EnumInfos.Count; i++) { var enumInfo = enumObject.EnumInfos[i]; sb.AppendLine(string.Format(enumInfoTemp, enumInfo.EnumName.PadRight(30), enumInfo.EnumValue.ToString().PadRight(9), enumInfo.Description)); } return(sb.ToString()); }
/// <summary> /// 获取查询范围内的枚举信息 /// </summary> private void getEnumInfo() { enumObjects = new List <EnumObject>(); int max = modeldll.Length; progressBar.Maximum = max; if (max == 0) { lblPercent.Text = "0/0"; } for (int i = 0; i < max; i++) { var file = modeldll[i]; lblTips.Text = "正在处理:" + file.Name; var assembly = Assembly.LoadFrom(file.FullName); var types = assembly.GetTypes(); foreach (var type in types) { if (type.Name.EndsWith("Enum")) { EnumObject enumObject = getEnumObject(type); enumObjects.Add(enumObject); txtContent.Text = txtContent.Text + "\r\n" + getEnumObjectString(enumObject); //让文本框获取焦点 txtContent.Focus(); //设置光标的位置到文本尾 txtContent.Select(txtContent.Text.Length, 0); //滚动到控件光标处 txtContent.ScrollToCaret(); Application.DoEvents(); } //设置进度 progressBar.Value = (i + 1); lblPercent.Text = (i + 1) + "/" + max; } } lblTips.Text = "处理完成,共计 " + enumObjects.Count + " 个枚举"; Application.DoEvents(); }
//设置行 private void export(EnumObject enumObject) { //获取枚举类 List <EnumInfo> enumInfos = enumObject.EnumInfos; // 创建新增行 for (var i = _row; i < enumInfos.Count + _row; i++) { EnumInfo enumInfo = enumInfos[i - _row]; IRow row1 = _sheet1.CreateRow(i); //新建单元格 ICell cell = row1.CreateCell(0); // 单元格赋值 cell.SetCellValue(enumObject.EnumObjectName); //新建单元格 ICell cell1 = row1.CreateCell(1); cell1.SetCellValue(enumInfo.Description); //枚举值 ICell cell2 = row1.CreateCell(2); cell2.SetCellValue(enumInfo.EnumName); //文本值 ICell cell3 = row1.CreateCell(3); cell3.SetCellValue(enumInfo.EnumValue); //使用实体 ICell cell4 = row1.CreateCell(4); cell4.SetCellValue(string.Join(", ", enumObject.EntityAndField)); } if (enumInfos.Count > 0) { // 合并单元格 _sheet1.AddMergedRegion(new CellRangeAddress(_row, _row + enumInfos.Count - 1, 0, 0)); _sheet1.AddMergedRegion(new CellRangeAddress(_row, _row + enumInfos.Count - 1, 4, 4)); } _row += enumInfos.Count; }