幽梦紫曦

XAF生成MarkDown

电脑版发表于:2020/11/27 15:57
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Validation;
using JGSoft.MES.Module.Extension.BaseBO;
using JGSoft.MES.Module.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace JGSoft.MES.Module.AttributeExtend
{
    public class ModelAttribute
    {
        public static void GetAttribute()
        {
            Dictionary<String, String> titlelist = new Dictionary<String, String>();
            #region 获取bo实体属性
            if (CommonTools.application != null)
            {
                var list = CommonTools.application.Model.BOModel.ToList();
                //添加md数据正文
                StringBuilder content = new StringBuilder();
                //菜单
                StringBuilder Header = new StringBuilder();
                //bo实体属性信息
                StringBuilder textcontent = new StringBuilder();
                foreach (IModelClass modelclass in list)
                {
                    var typeinfo = modelclass.TypeInfo;
                    bool issubclasssingle = typeinfo.Type.IsSubclassOf(typeof(SingleObject));
                    //跳过不继承的类
                    if (!issubclasssingle)
                    {
                        continue;
                    }
                    int count = 1;
                    foreach (IMemberInfo minfo in typeinfo.Members)
                    {
                        //自定义的公共属性 并有displayname的字段
                        if (minfo.IsPublic && minfo.IsProperty && minfo.FindAttribute<XafDisplayNameAttribute>() != null)
                        {
                            XafDisplayNameAttribute displays = minfo.Owner.FindAttribute<XafDisplayNameAttribute>();
                            if (!titlelist.ContainsKey(minfo.Owner.Name))
                            {
                                if (!string.IsNullOrEmpty(displays?.DisplayName))
                                {
                                    //标题
                                    content.Append("##" + minfo.Owner.Name + "(" + displays?.DisplayName + ")");
                                    //菜单
                                    Header.Append("| 序号 <div style=\"width:25pt\"> | 字段 | 字段名称 <div style=\"width:75pt\"> | 主键 | 外键 | 数据类型 | 是否只读 <div style=\"width:50pt\"> | 长度 <div style=\"width:30pt\"> | 是否必填 <div style=\"width:50pt\"> |");
                                    titlelist.Add(minfo.Owner.Name, displays?.DisplayName);
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(displays?.DisplayName))
                                    {
                                        //标题
                                        content.Append("##" + minfo.Owner.Name + "");
                                        //菜单
                                        Header.Append("| 序号 <div style=\"width:25pt\"> | 字段 | 字段名称 <div style=\"width:75pt\"> | 主键 | 外键 | 数据类型 | 是否只读 <div style=\"width:50pt\"> | 长度 <div style=\"width:30pt\"> | 是否必填 <div style=\"width:50pt\"> |");
                                        titlelist.Add(minfo.Owner.Name, displays?.DisplayName);
                                    }
                                }
                            }
                            var display = minfo.FindAttribute<XafDisplayNameAttribute>();
                            var require = minfo.FindAttribute<RuleRequiredFieldAttribute>();
                            bool required = require != null;
                            if (!string.IsNullOrEmpty(minfo.Owner.Name) && !string.IsNullOrEmpty(displays?.DisplayName))
                            {
                                textcontent.Append("\n");
                                //序号
                                textcontent.Append("| " + count + " |");
                                //字段
                                textcontent.Append(" " + minfo.Name + " |");
                                //字段名称
                                textcontent.Append(" " + display?.DisplayName + " |");
                                //主键
                                textcontent.Append(" " + minfo.IsKey + " |");
                                //外键
                                if (minfo.MemberType.FullName.Contains("JGSoft.MES.Module"))
                                {
                                    textcontent.Append(" True |");
                                }
                                else
                                {
                                    textcontent.Append(" False |");
                                }
                                //数据类型
                                if (minfo.MemberType.FullName.Contains("System.DateTime"))
                                {
                                    textcontent.Append(" System.DateTime |");
                                }
                                else
                                {
                                    textcontent.Append(" " + minfo.MemberType.FullName + " |");
                                }
                                //是否只读
                                textcontent.Append(" " + minfo.IsReadOnly + " |");
                                //长度
                                textcontent.Append(" " + minfo.Size + " |");
                                //是否必填
                                textcontent.Append(" " + required + " |");
                            }
                            count++;
                        }
                    }
                    if (!string.IsNullOrEmpty(Header.ToString()) && !string.IsNullOrEmpty(textcontent.ToString()))
                    {
                        content.Append("\n");
                        content.Append(Header);
                        content.Append("\n");
                        content.Append("|--------|--------|--------|--------|--------|--------|--------|--------|--------|");
                        content.Append(textcontent);
                        content.Append("\n");
                        Header.Length = 0;
                        textcontent.Length = 0;
                    }
                }
                string filepath = AppDomain.CurrentDomain.BaseDirectory + "ModelAttributeinfo";
                //如果不存在就创建file文件夹
                if (Directory.Exists(filepath) == false)
                {
                    Directory.CreateDirectory(filepath);
                }
                string path = filepath + "\\bo实体属性信息.md";
                FileStream file = null;
                //创建写入流文件存在则清空文件
                if (File.Exists(path))
                {
                    file = new FileStream(path, FileMode.Truncate, FileAccess.Write);
                }
                else
                {
                    //不存在则创建
                    file = new FileStream(path, FileMode.Create, FileAccess.Write);
                }
                //写入信息
                byte[] writeBytes = Encoding.UTF8.GetBytes(content.ToString());
                //写入文件流
                file.Write(writeBytes, 0, writeBytes.Length);
                //关闭
                file.Close();
            }
            #endregion
        }
    }
}


关于TNBLOG
TNBLOG,技术分享
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢