基本信息
源码名称:C# 将类库 生成帮助文档工具 源码
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2015-06-08
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Xml; using System.Diagnostics; namespace ReflectionWinForm { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void BtnSelectFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "打开(Open)"; ofd.FileName = ""; ofd.InitialDirectory = Environment.SpecialFolder.Desktop.ToString();//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录 ofd.Filter = "All(*.*)|*.*|xml文件(*.xml)|*.xml"; ofd.ValidateNames = true; //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名 ofd.CheckFileExists = true; //验证路径有效性 ofd.CheckPathExists = true; //验证文件有效性 if (ofd.ShowDialog() == DialogResult.OK) { this.TxtFile.Text = ofd.SafeFileName; GenneralHtmlDocument(ofd.FileName); } } /// <summary> /// 生成HTML说明文档 /// </summary> /// <param name="filePath">文件路径</param> public void GenneralHtmlDocument(string filePath) { XmlDocument doc = new XmlDocument(); doc.Load(filePath); XmlElement root = doc.DocumentElement; XmlNodeList nodeList = root.SelectNodes("members")[0].SelectNodes("member"); XmlNode assembly = root.SelectNodes("assembly")[0].SelectNodes("name")[0]; StringBuilder htmlBuilder = new StringBuilder(); htmlBuilder.Append(@"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><html><head><title></title></head><body>"); htmlBuilder.Append("<table>"); foreach (XmlNode item in nodeList) { string name = item.Attributes["name"].InnerText; if (name.StartsWith("T"))//类标识 { string className = name.Substring(name.LastIndexOf(".") 1); htmlBuilder.Append(string.Format(@" <tr><td colspan='6'> <b>【命名空间:{0}】</b></td></tr>", assembly.InnerText.TrimSpace())); htmlBuilder.Append(string.Format(@" <tr><td colspan='6'> <b>类名:{0}</b></td></tr>", className)); } else if (name.StartsWith("M"))//方法标识 { string parmsTypeString = string.Empty; string[] parmsTypeArr = new string[] { }; if (name.Contains("(") & name.Contains(")")) { parmsTypeString = name.Substring(name.IndexOf("(") 1, name.LastIndexOf(")") - (name.IndexOf("(") 1)); parmsTypeArr = parmsTypeString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); } name = name.Contains("(") ? name.Substring(0, name.LastIndexOf("(")) : name; string methodName = name.Substring(name.LastIndexOf(".") 1); string methodDescription = item.SelectNodes("summary")[0].InnerText.TrimSpace(); htmlBuilder.Append(string.Format(@"<tr><td class='tdleft'>方法名</td><td>{0}</td><td class='tdleft'>方法描述</td><td colspan='2'>{1}</td></tr>", methodName, methodDescription)); htmlBuilder.Append("<tr> <td class='tdleft'>参数名称 </td> <td class='tdleft'> 参数类型 </td> <td class='tdleft'> 是否必须 </td><td class='tdleft'>参数描述</td><td class='tdleft'>默认值</td> </tr>"); XmlNodeList parmsList = item.SelectNodes("param"); for (int i = 0; i < parmsList.Count; i ) { string parmsName = parmsList[i].Attributes["name"].InnerText.TrimSpace(); string parmsType = i < parmsTypeArr.Length ? parmsTypeArr[i].TrimSpace() : ""; string parmsNotes = parmsList[i].InnerText.TrimSpace(); string parmsIsNull = "Null"; string parmsDefaultValue = GetTypeDefaultValue(parmsType); htmlBuilder.Append(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>", parmsName, parmsType, parmsIsNull, parmsNotes, parmsDefaultValue)); } if (item.SelectNodes("returns").Count > 0) { string retrun = item.SelectNodes("returns")[0].InnerText.TrimSpace(); htmlBuilder.Append(string.Format("<tr><td class='tdleft'>返回结果:</td><td colspan='5'>{0}</td></tr>", retrun)); } htmlBuilder.Append("<tr><td colspan='6'></td></tr>"); } } htmlBuilder = new StringBuilder(htmlBuilder.ToString().Substring(0, htmlBuilder.ToString().Length - "<tr><td colspan='6'></td></tr>".Length)); htmlBuilder.Append("</table>"); htmlBuilder.Append("<style type='text/css'>"); htmlBuilder.Append(".tdleft{ background-color: #F2F5A9;width:10%;}"); htmlBuilder.Append(".tdvalue{ width:200px;}"); //表格加边框样式 htmlBuilder.Append(@"table{border-collapse:collapse;border-spacing:0;border-left:1px solid #888;border-top:1px solid #888;background:#efefef;}th,td{border-right:1px solid #888;border-bottom:1px solid #888;padding:5px 15px;}th{font-weight:bold;background:#ccc;}"); htmlBuilder.Append("</style>"); htmlBuilder.Append(@"</body></html>"); string documentFilePath = Environment.CurrentDirectory Path.DirectorySeparatorChar "document.htm"; SaveStringToFile(documentFilePath, htmlBuilder.ToString()); ShowFile(documentFilePath); } private void ShowFile(string filePath) { try { Process.Start("Explorer.exe", filePath); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 保存到文件 /// </summary> /// <param name="fileName">文件名</param> /// <param name="content">文件内容</param> public static void SaveStringToFile(string fileName, string content) { try { FileStream fs = new FileStream(fileName, FileMode.Create); Encoding encode = Encoding.GetEncoding("gb2312"); //获得字节数组 byte[] data = encode.GetBytes(content); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public static string GetTypeDefaultValue(string type) { string defaultValue = string.Empty; switch (type) { case "System.Decimal": case "System.Int64": case "System.Int32": defaultValue = "0"; break; case "System.String": defaultValue = ""; break; case "System.DateTime": defaultValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); break; default: defaultValue = ""; break; } return defaultValue; } private void BtnViewHtmlDocument_Click(object sender, EventArgs e) { string documentFilePath = Environment.CurrentDirectory Path.DirectorySeparatorChar "document.htm"; if (File.Exists(documentFilePath)) { try { Process.Start("Explorer.exe", Environment.CurrentDirectory); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("文件不存在!"); } } } public static class StringExtendMethod { /// <summary> /// 移除多余空格 /// </summary> /// <param name="orgStr"></param> /// <returns></returns> public static string TrimSpace(this string orgStr) { return orgStr.Replace("\r", "").Replace("\n", "").Replace(" ", ""); } } }