码迷,mamicode.com
首页 > 其他好文 > 详细

summary

时间:2018-08-03 18:49:25      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:hash   class   library   domain   trim   model   generic   methods   ace   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ConsoleApplication31
{


    /// <summary>
    /// 类注释辅助类
    /// </summary>
    public class ClassSummary
    {
        /// <summary>
        /// 类注释的构造函数
        /// </summary>
        public ClassSummary()
        {
            MethodSummaryDictionary = new Dictionary<string, string>();

        }
        /// <summary>
        /// 类名
        /// </summary>
        public string ClassName { get; set; }
        /// <summary>
        /// 每个类有N个方法 key:方法名 value:方法注释
        /// </summary>
        public Dictionary<string, string> MethodSummaryDictionary { get; set; }

    }
    class Program
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ExtractInterfaceMthod2();
            Console.Read();
        }

        private static void ExtractInterfaceMthod()
        {
            var types = Assembly.Load("ClassLibrary1").GetTypes();
            StringBuilder sb = new StringBuilder();
            string str = "";
            string calfunc = "";
            foreach (var item in types)
            {
                if (item.Name == "IUser1EngineController")
                {
                    var methods = item.GetMethods().ToList();

                    foreach (MethodInfo m in methods)
                    {
                        var returntype = m.ReturnType.Name;
                        string methodName = m.Name;
                        if (string.Compare(returntype, "void", true) == 0)
                        {
                            returntype = "void";
                        }
                        str = $"public {returntype} {methodName} ( ";
                        calfunc = $" {methodName} ( ";

                        var parameters = m.GetParameters().ToList();
                        foreach (ParameterInfo parameter in parameters)
                        {
                            string pn = parameter.Name;
                            string ptype = parameter.ParameterType.ToString();
                            bool isout = false;
                            if (ptype.EndsWith("&"))
                            {
                                isout = true;
                                ptype = $"out {ptype.TrimEnd(‘&‘)}";
                            }
                            str += ptype + " " + pn + ",";
                            if (isout)
                            {
                                pn = "out " + pn;
                            }
                            calfunc += pn + ",";
                        }
                        str = str.TrimEnd(,);
                        string isreturn = returntype != "void" ? "return" : "";
                        calfunc = $"{isreturn} valuationCal." + calfunc.TrimEnd(,) + ");";

                        str += ")\r\n{\r\n" + calfunc + "\r\n}";

                        sb.Append("\r\n" + str);

                    }


                }

            }
            var str1 = sb.ToString();
            Console.WriteLine(str1);
        }

        private static void ExtractInterfaceMthod2()
        {

            var types = Assembly.Load("ClassLibrary1").GetTypes();
            StringBuilder sb = new StringBuilder();
            string str = "";
            string calfunc = "";
            Dictionary<string, ClassSummary> methodDictionary = new Dictionary<string, ClassSummary>();
            ClassSummary cs = null;
            foreach (var item in types)
            {



                if (item.Name == "User2EngineController")
                {

                    methodDictionary[item.Name] = new ClassSummary();
                    cs = methodDictionary[item.Name];
                    cs.ClassName = item.Name;
                    XmlDocument xmlDoc = new XmlDocument();
                    string configPath = AppDomain.CurrentDomain.BaseDirectory + @"ClassLibrary1.xml";
                    xmlDoc.Load(configPath);
                    XmlNode doc = xmlDoc.SelectSingleNode("doc");
                    XmlNode members = doc.SelectSingleNode("members");
                    XmlNodeList memberList = members.SelectNodes("member");
                    foreach (XmlNode member in memberList)
                    {
                        string summary = member.InnerXml;
                        string oriContent= member.SelectSingleNode("summary").InnerText.Replace("\r\n", "").Replace("\r\n","").Replace(" ","");
                        string content = "///" + oriContent;
         
                        string fullName = member.Attributes.GetNamedItem("name").InnerText.ToString();
                        string left = fullName.Split(()[0];
                        string methodName = left.Split(.)[left.Split(.).Length - 1];

                        cs.MethodSummaryDictionary[methodName] = summary.Replace(oriContent,content);


                    }
                    //载入xml文件名
                    var methods = item.GetMethods().ToList();

                    foreach (MethodInfo m in methods)
                    {
                        var returntype = m.ReturnType.Name;
                        string methodName = m.Name;
                        if (methodName == "ToString" || methodName == "GetHashCode" || methodName == "Equals" || methodName == "GetType")
                        {
                            continue;
                        }
                        if (string.Compare(returntype, "void", true) == 0)
                        {
                            returntype = "void";
                        }
                        str = $" {returntype} {methodName} ( ";
                        calfunc = $" {methodName} ( ";

                        var parameters = m.GetParameters().ToList();
                        foreach (ParameterInfo parameter in parameters)
                        {

                            string pn = parameter.Name;
                            string ptype = parameter.ParameterType.ToString();
                            bool isout = false;
                            if (ptype.EndsWith("&"))
                            {
                                isout = true;
                                ptype = $"out {ptype.TrimEnd(‘&‘)}";
                            }
                            str += ptype + " " + pn + ",";
                            if (isout)
                            {
                                pn = "out " + pn;
                            }
                            calfunc += pn + ",";
                        }
                        str = str.TrimEnd(,);
                        string isreturn = returntype != "void" ? "return" : "";
                        calfunc = calfunc.TrimEnd(,) + ");";

                        str += ");";
                        string summary = cs.MethodSummaryDictionary[m.Name];
                        summary = summary.Replace("<", "\r\n<");
                        summary = summary.Replace("<", "///<");
                        sb.Append("\r\n" + summary + "\r\n" + str);

                    }


                }

            }
            var str1 = sb.ToString();
            Console.WriteLine(str1);
        }

    }
}

 

summary

标签:hash   class   library   domain   trim   model   generic   methods   ace   

原文地址:https://www.cnblogs.com/kexb/p/9415676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!