mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	- 增加 [Description] 元数据注释,优先级低于 c# 代码注释;
This commit is contained in:
		@@ -371,6 +371,48 @@ namespace FreeSql.Internal
 | 
			
		||||
            return iidx == 1 ? sb.Remove(0, 5).Remove(sb.Length - 1, 1).ToString() : sb.Remove(0, 4).ToString();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 动态读取 DescriptionAttribute 注释文本
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="type"></param>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public static Dictionary<string, string> GetPropertyCommentByDescriptionAttribute(Type type)
 | 
			
		||||
        {
 | 
			
		||||
            var dic = new Dictionary<string, string>();
 | 
			
		||||
            GetDydesc(null); //class注释
 | 
			
		||||
 | 
			
		||||
            var props = type.GetPropertiesDictIgnoreCase().Values;
 | 
			
		||||
            foreach (var prop in props)
 | 
			
		||||
                GetDydesc(prop);
 | 
			
		||||
 | 
			
		||||
            return dic;
 | 
			
		||||
 | 
			
		||||
            void GetDydesc(PropertyInfo prop)
 | 
			
		||||
            {
 | 
			
		||||
                object[] attrs = null;
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    attrs = prop == null ? 
 | 
			
		||||
                        type.GetCustomAttributes(false).ToArray() : 
 | 
			
		||||
                        prop.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常
 | 
			
		||||
                }
 | 
			
		||||
                catch { }
 | 
			
		||||
 | 
			
		||||
                var dyattr = attrs?.Where(a => {
 | 
			
		||||
                    return ((a as Attribute)?.TypeId as Type)?.Name == "DescriptionAttribute";
 | 
			
		||||
                }).FirstOrDefault();
 | 
			
		||||
                if (dyattr != null)
 | 
			
		||||
                {
 | 
			
		||||
                    var valueProp = dyattr.GetType().GetProperties().Where(a => a.PropertyType == typeof(string)).FirstOrDefault();
 | 
			
		||||
                    var comment = valueProp?.GetValue(dyattr, null)?.ToString();
 | 
			
		||||
                    if (string.IsNullOrEmpty(comment) == false)
 | 
			
		||||
                        dic.Add(prop == null ? 
 | 
			
		||||
                            "" : 
 | 
			
		||||
                            prop.Name, comment);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 通过属性的注释文本,通过 xml 读取
 | 
			
		||||
        /// </summary>
 | 
			
		||||
@@ -378,6 +420,8 @@ namespace FreeSql.Internal
 | 
			
		||||
        /// <returns>Dict:key=属性名,value=注释</returns>
 | 
			
		||||
        public static Dictionary<string, string> GetProperyCommentBySummary(Type type)
 | 
			
		||||
        {
 | 
			
		||||
            if (type.Assembly.IsDynamic) return null;
 | 
			
		||||
            //动态生成的程序集,访问不了 Assembly.Location/Assembly.CodeBase
 | 
			
		||||
            var regex = new Regex(@"\.(dll|exe)", RegexOptions.IgnoreCase);
 | 
			
		||||
            var xmlPath = regex.Replace(type.Assembly.Location, ".xml");
 | 
			
		||||
            if (File.Exists(xmlPath) == false)
 | 
			
		||||
 
 | 
			
		||||
@@ -69,7 +69,10 @@ namespace FreeSql.Internal
 | 
			
		||||
            var propsLazy = new List<NaviteTuple<PropertyInfo, bool, bool, MethodInfo, MethodInfo>>();
 | 
			
		||||
            var propsNavObjs = new List<PropertyInfo>();
 | 
			
		||||
            var propsComment = CommonUtils.GetProperyCommentBySummary(entity);
 | 
			
		||||
            var propsCommentByDescAttr = CommonUtils.GetPropertyCommentByDescriptionAttribute(entity);
 | 
			
		||||
            trytb.Comment = propsComment != null && propsComment.TryGetValue("", out var tbcomment) ? tbcomment : "";
 | 
			
		||||
            if (string.IsNullOrEmpty(trytb.Comment) && propsCommentByDescAttr != null && propsCommentByDescAttr.TryGetValue("", out tbcomment)) trytb.Comment = tbcomment;
 | 
			
		||||
 | 
			
		||||
            var columnsList = new List<ColumnInfo>();
 | 
			
		||||
            foreach (var p in trytb.Properties.Values)
 | 
			
		||||
            {
 | 
			
		||||
@@ -148,6 +151,9 @@ namespace FreeSql.Internal
 | 
			
		||||
                };
 | 
			
		||||
                if (propsComment != null && propsComment.TryGetValue(p.Name, out var trycomment))
 | 
			
		||||
                    col.Comment = trycomment;
 | 
			
		||||
                if (string.IsNullOrEmpty(col.Comment) && propsCommentByDescAttr != null && propsCommentByDescAttr.TryGetValue(p.Name, out trycomment))
 | 
			
		||||
                    col.Comment = trycomment;
 | 
			
		||||
 | 
			
		||||
                if (colattr.IsIgnore)
 | 
			
		||||
                {
 | 
			
		||||
                    trytb.ColumnsByCsIgnore.Add(p.Name, col);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user