技术文章 > [原创]C#代码注释文字自动提取,与他人共享DLL

[原创]C#代码注释文字自动提取,与他人共享DLL

2018-04-24 16:51

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:


C# 开发过程中经常需要与大家共享一些 DLL ;但是如果都去看 .CS ,未免太累人了。
如果大家都遵守 XML /// 形式的注释,那末可以用下面的程序一次性提取相关 XML 注释。
别人只需要阅读该程序生成的 HTML 文件即可(或许可以转存到 WORD PDF 等文件中)。

<%@ Page Language=“C#“ AutoEventWireup=“true“ EnableViewState=“false“ %>
<%@ Import Namespace=“System“ %>
<%@ Import Namespace=“System.Data“ %>
<%@ Import Namespace=“System.Configuration“ %>
<%@ Import Namespace=“System.Web“ %>
<%@ Import Namespace=“System.Web.Security“ %>
<%@ Import Namespace=“System.Web.UI“ %>
<%@ Import Namespace=“System.Web.UI.WebControls“ %>
<%@ Import Namespace=“System.Web.UI.WebControls.WebParts“ %>
<%@ Import Namespace=“System.Web.UI.HtmlControls“ %>
<%@ Import Namespace=“System.Runtime.Remoting“ %>
<%@ Import Namespace=“System.Runtime.Remoting.Lifetime“ %>
<%@ Import Namespace=“System.IO“ %>
<script language=“C#“ runat=“server“>

string pContent;

string KeywordFormat(string inputString)
{
string ks = “public,private,static,class,virtual,override,new,void,string,int,bool,double,float,char,byte,DateTime,set,get“;
string[] ka = ks.Split(“,“);
string outputString = inputString;
foreach(string kg in ka)
{
outputString = outputString.Replace(kg + “ “,“<font class=\“keyword\“>“ + kg + “ </font>“);
outputString = outputString.Replace(“(“+ kg + “)“,“(<font class=\“keyword\“>“ + kg + “</font>)“);
outputString = outputString.Replace( kg + “[“,“<font class=\“keyword\“>“ + kg + “</font>[“);
}
return outputString;
}

protected void Page_Load(object sender, EventArgs e)
{
pContent = ““;
// 用于保存结果
StringBuilder sb = new StringBuilder();
// 用于保存注释
StringBuilder sm = new StringBuilder();

// 遍历指定目录下的全部 CS 文件
DirectoryInfo csfile = new DirectoryInfo(Server.MapPath(“/App_Code“));
foreach(System.IO.FileInfo fi in csfile.GetFiles())
{
int depth=0;

sb.AppendLine(“<div style=\“padding:4px;margin:10px;padding-left:20px;border:solid 1px #AAAAAA;background-color:#F0F0F0\“>“);
sb.AppendLine(“<font class=\“file\“><b>/App_Code/“ + fi.Name.ToString() + “</font> “);

StreamReader TxtReader = new StreamReader(Server.MapPath(“/App_Code/“ + fi.Name.ToString()), System.Text.Encoding.Default);
string FileContent = TxtReader.ReadToEnd();
TxtReader.Close();

// 按行分割
string[] fLines = Regex.Split(FileContent, “\r\n“);

// 未进入注释段
int ok=0;
foreach(string ls in fLines)
{
string vls = ls;

// 如果是 namespace
if(vls.Length>10)
{
if(vls.Substring(0,9) == “namespace“)
{
sb.AppendLine(“ ~~~ <font class=\“namespace\“>“ + vls.Substring(10) + “</font></b></div>“);
depth = 1;
//sb.AppendLine(“<div style=\“padding:4px;margin:10px;padding-left:“ + (depth*40+20) + “px\“>“);
continue;
}
}

vls = vls.Trim();
if(vls.Length>=3)
{
if(vls.Substring(0,3) == “///“)
{
//sb.AppendLine(“<font class=\“summary\“>“);
// 如果是说明行,跳过即可
if(vls.Contains(“<summary>“))
{
// 已进入注释段
ok=1;
sm.Append(“功能:“);
continue;
}

if(vls.Contains(“</summary>“))
{
// 已进入注释段
ok=1;
continue;
}

string gls = vls.Replace(“///“,““).Trim();
if(gls.Length>2)
{
vls = vls.Replace(“<param name=\““,“参数:“);
vls = vls.Replace(“\“>“,“ “);
vls = vls.Replace(“</param>“,““);
vls = vls.Replace(“<returns></returns>“,““);
vls = vls.Replace(“<returns>“,“返回:“);
vls = vls.Replace(“</returns>“,““);
vls = vls.Replace(“///“, ““).Trim();
if(vls != ““)
{
sm.AppendLine(vls.Trim() + “<br>“);
}
}
// 已进入注释段
ok = 1;
}
else
{
// 如果已经进入注释段
if(ok==1)
{
// 如果是类
if(ls.Contains(“ class “) )
{
sb.AppendLine(“<div style=\“padding:4px;margin:4px;padding-left:60px\“>“);
sb.AppendLine(“<font class=\“summary\“>“ + sm.ToString() + “</font>“);
sb.AppendLine(“<font class=\“class\“>“ + KeywordFormat(ls.Trim()) + “</font>“);
sb.AppendLine(“</div>“);
}
else
{
sb.AppendLine(“<div style=\“padding:4px;margin:4px;padding-left:100px\“>“);
sb.AppendLine(“<font class=\“summary\“>“ + sm.ToString() + “</font>“);
string kls = ls.Trim();
if(kls.Substring(kls.Length-1,1)==“)“)
sb.AppendLine( “<font class=\“function\“>方法:“ + KeywordFormat(kls) + “</font>“);
else
sb.AppendLine( “<font class=\“property\“>属性:“ + KeywordFormat(kls) + “</font>“);
sb.AppendLine(“</div>“);
}
ok=0;
sm.Remove(0,sm.Length);
}
}
}
}
if(depth>0) sb.AppendLine(“</div>“);
if(depth>1) sb.AppendLine(“</div>“);
sb.AppendLine(“</div>“);
}
pContent = sb.ToString();
}

</script>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN“ “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=“http://www.w3.org/1999/xhtml“ >
<head>
<title>C#类地图:联高软件Legalsoft.com.cn</title>
<meta http-equiv=“content-type“ content=“text/html; charset=gb2312“>
<style type=“text/css“>
<!--

.keyword { font-size:12px;color:#0000EE;font-weight:normal; }
.file { font-size:16px;color:#000000; }
.namespace { font-size:16px;color:#990000; }
.class { font-size:14px;color:#AA0000;font-weight:bold; }
.function { font-size:12px;color:#333333;font-weight:bold; }
.property { font-size:12px;color:#888800;font-weight:bold; }
.summary { font-size:12px;color:#888888; }

-->
</style>
</head>
<body style=“font-size:12px;font-family:arial,宋体;line-height:16px;“>
<% =pContent %>
</body>
</html>