IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> C#读取注释的方法 -> 正文阅读

[游戏开发]C#读取注释的方法

一、在C#中,注释不是可执行代码的一部分,因此注释不会被编译到程序集中去,但是我们可以提取注释【右键项目】-【属性】-【生成】-【输出】-【Xml文档文件】

二、创建一个辅助类:

/// <summary>
    /// 注释辅助类
    /// </summary>
    public class XmlCommentHelper
    {
        private static Regex RefTagPattern = new Regex(@"<(see|paramref) (name|cref)=""([TPF]{1}:)?(?<display>.+?)"" ?/>");
        private static Regex CodeTagPattern = new Regex(@"<c>(?<display>.+?)</c>");
        private static Regex ParaTagPattern = new Regex(@"<para>(?<display>.+?)</para>", RegexOptions.Singleline);

        List<XPathNavigator> navigators = new List<XPathNavigator>();

        /// <summary>
        /// 从当前dll文件中加载所有的xml文件
        /// </summary>
        public void LoadAll()
        {
            var files = Directory.GetFiles(Directory.GetCurrentDirectory());
            foreach (var file in files)
            {
                if (string.Equals(Path.GetExtension(file), ".xml", StringComparison.OrdinalIgnoreCase))
                {
                    Load(file);
                }
            }
        }
        /// <summary>
        /// 从xml中加载
        /// </summary>
        /// <param name="xmls"></param>
        public void LoadXml(params string[] xmls)
        {
            foreach (var xml in xmls)
            {
                Load(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
            }
        }
        /// <summary>
        /// 从文件中加载
        /// </summary>
        /// <param name="xmlFiles"></param>
        public void Load(params string[] xmlFiles)
        {
            foreach (var xmlFile in xmlFiles)
            {
                var doc = new XPathDocument(xmlFile);
                navigators.Add(doc.CreateNavigator());
            }
        }
        /// <summary>
        /// 从流中加载
        /// </summary>
        /// <param name="streams"></param>
        public void Load(params Stream[] streams)
        {
            foreach (var stream in streams)
            {
                var doc = new XPathDocument(stream);
                navigators.Add(doc.CreateNavigator());
            }
        }

        /// <summary>
        /// 读取类型中的注释
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="xPath">注释路径</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public string GetTypeComment(Type type, string xPath = "summary", bool humanize = true)
        {
            var typeMemberName = GetMemberNameForType(type);
            return GetComment(typeMemberName, xPath, humanize);
        }
        /// <summary>
        /// 读取字段或者属性的注释
        /// </summary>
        /// <param name="fieldOrPropertyInfo">字段或者属性</param>
        /// <param name="xPath">注释路径</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public string GetFieldOrPropertyComment(MemberInfo fieldOrPropertyInfo, string xPath = "summary", bool humanize = true)
        {
            var fieldOrPropertyMemberName = GetMemberNameForFieldOrProperty(fieldOrPropertyInfo);
            return GetComment(fieldOrPropertyMemberName, xPath, humanize);
        }
        /// <summary>
        /// 读取方法中的注释
        /// </summary>
        /// <param name="methodInfo">方法</param>
        /// <param name="xPath">注释路径</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public string GetMethodComment(MethodInfo methodInfo, string xPath = "summary", bool humanize = true)
        {
            var methodMemberName = GetMemberNameForMethod(methodInfo);
            return GetComment(methodMemberName, xPath, humanize);
        }
        /// <summary>
        /// 读取方法中的返回值注释
        /// </summary>
        /// <param name="methodInfo">方法</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public string GetMethodReturnComment(MethodInfo methodInfo, bool humanize = true)
        {
            return GetMethodComment(methodInfo, "returns", humanize);
        }
        /// <summary>
        /// 读取参数的注释
        /// </summary>
        /// <param name="parameterInfo">参数</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public string GetParameterComment(ParameterInfo parameterInfo, bool humanize = true)
        {
            if (!(parameterInfo.Member is MethodInfo methodInfo)) return string.Empty;

            var methodMemberName = GetMemberNameForMethod(methodInfo);
            return GetComment(methodMemberName, $"param[@name='{parameterInfo.Name}']", humanize);
        }
        /// <summary>
        /// 读取方法的所有参数的注释
        /// </summary>
        /// <param name="methodInfo">方法</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public Dictionary<string, string> GetParameterComments(MethodInfo methodInfo, bool humanize = true)
        {
            var parameterInfos = methodInfo.GetParameters();
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (var parameterInfo in parameterInfos)
            {
                dict[parameterInfo.Name] = GetParameterComment(parameterInfo, humanize);
            }
            return dict;
        }
        /// <summary>
        /// 读取指定名称节点的注释
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <param name="xPath">注释路径</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public string GetComment(string name, string xPath, bool humanize = true)
        {
            foreach (var _xmlNavigator in navigators)
            {
                var typeSummaryNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{name}']/{xPath.Trim('/', '\\')}");

                if (typeSummaryNode != null)
                {
                    return humanize ? Humanize(typeSummaryNode.InnerXml) : typeSummaryNode.InnerXml;
                }
            }

            return string.Empty;
        }
        /// <summary>
        /// 读取指定节点的summary注释
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public string GetSummary(string name, bool humanize = true)
        {
            return GetComment(name, "summary", humanize);
        }
        /// <summary>
        /// 读取指定节点的example注释
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <param name="humanize">可读性优化(比如:去掉xml标记)</param>
        /// <returns></returns>
        public string GetExample(string name, bool humanize = true)
        {
            return GetComment(name, "example", humanize);
        }
        /// <summary>
        /// 获取方法的节点名称
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public string GetMemberNameForMethod(MethodInfo method)
        {
            var builder = new StringBuilder("M:");

            builder.Append(QualifiedNameFor(method.DeclaringType));
            builder.Append($".{method.Name}");

            var parameters = method.GetParameters();
            if (parameters.Any())
            {
                var parametersNames = parameters.Select(p =>
                {
                    return p.ParameterType.IsGenericParameter
                        ? $"`{p.ParameterType.GenericParameterPosition}"
                        : QualifiedNameFor(p.ParameterType, expandGenericArgs: true);
                });
                builder.Append($"({string.Join(",", parametersNames)})");
            }

            return builder.ToString();
        }
        /// <summary>
        /// 获取类型的节点名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GetMemberNameForType(Type type)
        {
            var builder = new StringBuilder("T:");
            builder.Append(QualifiedNameFor(type));

            return builder.ToString();
        }
        /// <summary>
        /// 获取字段或者属性的节点名称
        /// </summary>
        /// <param name="fieldOrPropertyInfo"></param>
        /// <returns></returns>
        public string GetMemberNameForFieldOrProperty(MemberInfo fieldOrPropertyInfo)
        {
            var builder = new StringBuilder(((fieldOrPropertyInfo.MemberType & MemberTypes.Field) != 0) ? "F:" : "P:");
            builder.Append(QualifiedNameFor(fieldOrPropertyInfo.DeclaringType));
            builder.Append($".{fieldOrPropertyInfo.Name}");

            return builder.ToString();
        }

        private string QualifiedNameFor(Type type, bool expandGenericArgs = false)
        {
            if (type.IsArray)
                return $"{QualifiedNameFor(type.GetElementType(), expandGenericArgs)}[]";

            var builder = new StringBuilder();

            if (!string.IsNullOrEmpty(type.Namespace))
                builder.Append($"{type.Namespace}.");

            if (type.IsNested)
            {
                builder.Append($"{string.Join(".", GetNestedTypeNames(type))}.");
            }

            if (type.IsConstructedGenericType && expandGenericArgs)
            {
                var nameSansGenericArgs = type.Name.Split('`').First();
                builder.Append(nameSansGenericArgs);

                var genericArgsNames = type.GetGenericArguments().Select(t =>
                {
                    return t.IsGenericParameter
                        ? $"`{t.GenericParameterPosition}"
                        : QualifiedNameFor(t, true);
                });

                builder.Append($"{{{string.Join(",", genericArgsNames)}}}");
            }
            else
            {
                builder.Append(type.Name);
            }

            return builder.ToString();
        }
        private IEnumerable<string> GetNestedTypeNames(Type type)
        {
            if (!type.IsNested || type.DeclaringType == null) yield break;

            foreach (var nestedTypeName in GetNestedTypeNames(type.DeclaringType))
            {
                yield return nestedTypeName;
            }

            yield return type.DeclaringType.Name;
        }
        private string Humanize(string text)
        {
            if (text == null)
                throw new ArgumentNullException("text");

            //Call DecodeXml at last to avoid entities like &lt and &gt to break valid xml       
            text = NormalizeIndentation(text);
            text = HumanizeRefTags(text);
            text = HumanizeCodeTags(text);
            text = HumanizeParaTags(text);
            text = DecodeXml(text);
            return text;
        }
        private string NormalizeIndentation(string text)
        {
            string[] lines = text.Split('\n');
            string padding = GetCommonLeadingWhitespace(lines);

            int padLen = padding == null ? 0 : padding.Length;

            // remove leading padding from each line
            for (int i = 0, l = lines.Length; i < l; ++i)
            {
                string line = lines[i].TrimEnd('\r'); // remove trailing '\r'

                if (padLen != 0 && line.Length >= padLen && line.Substring(0, padLen) == padding)
                    line = line.Substring(padLen);

                lines[i] = line;
            }

            // remove leading empty lines, but not all leading padding
            // remove all trailing whitespace, regardless
            return string.Join("\r\n", lines.SkipWhile(x => string.IsNullOrWhiteSpace(x))).TrimEnd();
        }
        private string GetCommonLeadingWhitespace(string[] lines)
        {
            if (null == lines)
                throw new ArgumentException("lines");

            if (lines.Length == 0)
                return null;

            string[] nonEmptyLines = lines
                .Where(x => !string.IsNullOrWhiteSpace(x))
                .ToArray();

            if (nonEmptyLines.Length < 1)
                return null;

            int padLen = 0;

            // use the first line as a seed, and see what is shared over all nonEmptyLines
            string seed = nonEmptyLines[0];
            for (int i = 0, l = seed.Length; i < l; ++i)
            {
                if (!char.IsWhiteSpace(seed, i))
                    break;

                if (nonEmptyLines.Any(line => line[i] != seed[i]))
                    break;

                ++padLen;
            }

            if (padLen > 0)
                return seed.Substring(0, padLen);

            return null;
        }
        private string HumanizeRefTags(string text)
        {
            return RefTagPattern.Replace(text, (match) => match.Groups["display"].Value);
        }
        private string HumanizeCodeTags(string text)
        {
            return CodeTagPattern.Replace(text, (match) => "{" + match.Groups["display"].Value + "}");
        }
        private string HumanizeParaTags(string text)
        {
            return ParaTagPattern.Replace(text, (match) => "<br>" + match.Groups["display"].Value);
        }
        private string DecodeXml(string text)
        {
            return System.Net.WebUtility.HtmlDecode(text);
        }
    }
  比如有下面的类注释:  

    /// <summary>
    /// MyClass注释
    /// </summary>
    public class MyClass
    {
        /// <summary>
        /// FieldName1字段注释
        /// </summary>
        int FieldName1;
        /// <summary>
        /// FieldName2字段注释
        /// </summary>
        string FieldName2;

        /// <summary>
        /// PropertyName1属性注释
        /// </summary>
        public int PropertyName1 { get; set; }
        /// <summary>
        /// PropertyName2属性注释
        /// </summary>
        public string PropertyName2 { get; set; }

        /// <summary>
        /// MyMethod方法注释
        /// </summary>
        /// <param name="intParameter">整型参数<see cref="int"/></param>
        /// <param name="stringParameter">字符串类型参数<see cref="string"/></param>
        /// <returns>返回值类型是字符串</returns>
        public string MyMethod(int intParameter, string stringParameter)
        {
            return string.Empty;
        }
    }

三、使用方式

static void Main(string[] args)
    {
        var xmlCommentHelper = new XmlCommentHelper();
        xmlCommentHelper.LoadAll();

        Type type = typeof(MyClass);
        var typeComment = xmlCommentHelper.GetTypeComment(type);
        Console.WriteLine($"{type.Name}的注释:{typeComment}");

        var fields = type.GetFields(bindingAttr: System.Reflection.BindingFlags.NonPublic);
        foreach (var field in fields)
        {
            var fieldComment = xmlCommentHelper.GetFieldOrPropertyComment(field);
            Console.WriteLine($"{field.Name}字段的注释:{fieldComment}");
        }

        var properties = type.GetProperties();
        foreach (var property in properties)
        {
            var propertyComment = xmlCommentHelper.GetFieldOrPropertyComment(property);
            Console.WriteLine($"{property.Name}属性的注释:{propertyComment}");
        }

        var method = type.GetMethod(nameof(MyClass.MyMethod));
        var methodComment = xmlCommentHelper.GetMethodComment(method);
        Console.WriteLine($"{nameof(MyClass.MyMethod)}方法的注释:{methodComment}");

        var dict = xmlCommentHelper.GetParameterComments(method);
        foreach (var pair in dict)
        {
            Console.WriteLine($"{nameof(MyClass.MyMethod)}方法的参数{pair.Key}注释:{pair.Value}");
        }
    }

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-03-30 18:59:49  更:2022-03-30 19:00:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 20:30:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码