C#(CSharp) | VC/C++ | C++Builder | ASP(ASP.NET) | SQL Server | OpenGL | CMM | 网站开发SEO | 数控技术 | 地理信息系统 | WINDOWS操作系统 |
联高软件 > 技术文档 > C# > C#代码注释文字自动提取,与他人共享DLL[原创] C#软件开发参考文档

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

发表:联高软件www.legalsoft.com.cn,本文被阅读:4245

摘要:文章:C#代码注释文字自动提取,与他人共享DLL[原创] 摘要:C#开发过程中经常需要与大家共享一些DLL;但是如果都去看.CS,未免太累人了。如果大家都遵守XML形式的注释,那末可以,发表于北京联高软件有限公司技术文章栏目,代码以高亮显示。
关键字:注释, 原创, dll, 文字, 代码, 提取, 共享, 自动, gt, font, vls, sb, appendline, namespace, class, string, system, import, replace, div, padding, size



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>
[C#] C#焦点事件中的Validating处理方法 (4431)
[C#] C#中string与byte[]的转换帮助类 (3337)
[C#] 使用 HttpContext.RewritePath 来配合 Server.Transfer/Execute (3793)
[C#] 理解C#中的string类型 (2408)
[C#] 对C#开发的两个基本原则的深入讨论 (2498)
[C#] 深入理解C#编程中的组件-事件-委托 (2138)
[C#] C#和SQL数据浏览分页 (1939)
[C#] C#高效分页代码(不用存储过程) (12511)
[C#] C#.NET数据库操作记要 (3141)
[C#] c#高性能在WEB端产生验证图片 (2450)
[C#] C#实现托盘图标动画显示 (947)
[C#] C#通过Url抓取网页内容(2) (10673)
[C#] C#如何把类CLASS实例进行序列化及反序列化为XML文件 (910)
[C#] 超强C#图片上传,加水印,自动生成缩略图源代码(联高原创) (5159)
[C#] 用C#实现把Office文件编码为xml的方法 (3088)
[C#] 用DES加密数据库信息,增强Asp.net的安全性(1)--分析 (1700)
[C#] 汉字转拼音缩写 (3188)
[C#] 对我的网页动态生成图片方法的修改 (3478)
[C#] 用C#实现Des加密和解密 (3402)
[C#] 汉字转换为UNICODE编码UNICODE编码转换为汉字的C#与JS源程序 (657)

www.315soft.com