1.调用
static void Main(string[] args)
{
Transform.ExtractInterfaceMthod("Holworth.Engine.IUser1", "IUser1EngineController", "valuationCal");
Transform.ExtractInterfaceMthod2("ValuationLib", "ValuationCal"); //ExtractInterfaceMthod2CallExsample ValuationCal
Console.Read();
return;
}
2.代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace ConsoleApplication1
{
public class Transform
{
/// <summary>
/// 根据接口生成方法
/// </summary>
/// <param name="moduleName">接口的程序集名称</param>
/// <param name="interfaceName">接口名</param>
/// <param name="caseName">实例的名称</param>
public static void ExtractInterfaceMthod(string moduleName,string interfaceName,string caseName)
{
var types = Assembly.Load(moduleName).GetTypes();
StringBuilder sb = new StringBuilder();
string str = "";
string calfunc = "";
Dictionary<string, ClassSummary> classSummaryDictionary = new Dictionary<string, ClassSummary>();
XmlDocument xmlDoc = new XmlDocument();
string configPath = AppDomain.CurrentDomain.BaseDirectory + $@"{moduleName}.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 fullName = member.Attributes.GetNamedItem("name").InnerText.ToString();
string left = fullName.Split('(')[0];
string className = left.Split('.')[left.Split('.').Length - 2];
string methodName = left.Split('.')[left.Split('.').Length - 1];
if (!classSummaryDictionary.ContainsKey(className))
{
classSummaryDictionary[className] = new ClassSummary();
}
ClassSummary cs = classSummaryDictionary[className];
string[] m2arr = fullName.Split(new string[] { methodName }, StringSplitOptions.None);
methodName = methodName + m2arr[1];
methodName = methodName.Replace("System.", "").Replace("@", "");
if (!cs.MethodSummaryDictionary.ContainsKey(methodName))
{
//GetCouponBondValue(Double[], Double[], Int32, Double, String, Double, DateTime, DateTime, DateTime, Int32, Int32, Boolean, Double)
cs.MethodSummaryDictionary[methodName] = new MethodSummary();
}
MethodSummary methodSummary = cs.MethodSummaryDictionary[methodName];
methodSummary.MethodZhushi = oriContent;
XmlNodeList paramNodeList = member.SelectNodes("param");
foreach (XmlNode paramNode in paramNodeList)
{
string parameterName = paramNode.Attributes.GetNamedItem("name").InnerText;
string paramSummary = paramNode.InnerText;
methodSummary.ParameterSummaryDictonary[parameterName] = paramSummary;
}
}
foreach (var item in types)
{
if (item.Name == interfaceName)
{
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} ( ";
char[] def = m.GetBaseDefinition().ToString().Trim().ToCharArray();
bool start = false;
bool first = true;
foreach (var c in def)
{
if (c == ' ' && first)
{
start = true;
methodName = "";
first = false;
continue;
}
if (start)
{
methodName += c.ToString();
}
}
methodName = methodName.Replace("System.", "").Replace(" ByRef", "");
methodName = methodName.Replace(" ", "");
string summary = "";
try
{
summary = "/// <summary>\r\n" + "/// " + classSummaryDictionary[item.Name].MethodSummaryDictionary[methodName].MethodZhushi + "\r\n" + "///</summary>";
foreach (var p in classSummaryDictionary[item.Name].MethodSummaryDictionary[methodName].ParameterSummaryDictonary)
{
summary += "\r\n/// <param name=" + "\"" + p.Key + "\"" + ">" + p.Value + " </param>";
}
}
catch (Exception ex)
{
summary = "/// <summary>\r\n" + "/// " + $"Badly formed XML comment ignored for member \"M: ValuationLib.ValuationCal.GetCouponBondValue(System.Double[], System.Double[], System.Int32, System.Double, System.String, System.Double, System.DateTime, System.DateTime, System.DateTime, System.Int32, System.Int32, System.Boolean, System.Double)" + "\r\n" + "///</summary>"; ;
}
var parameters = m.GetParameters().ToList();
foreach (ParameterInfo parameter in parameters)
{
string pn = parameter.Name;
string ptype = parameter.ParameterType.ToString();
bool isout = false;
string direction = "";
if (parameter.ParameterType.IsByRef)
{
direction = "ref ";
}
if (parameter.IsOut)
{
direction = "out ";
}
if (ptype.EndsWith("&"))
{
isout = true;
ptype = $"{direction} {ptype.TrimEnd('&')}";
}
str += ptype + " " + pn + ",";
if (isout)
{
pn = $"{direction} " + pn;
}
calfunc += pn + ",";
}
str = str.TrimEnd(',');
string isreturn = returntype != "void" ? "return" : "";
calfunc = $"{isreturn} {caseName}." + calfunc.TrimEnd(',') + ");";
str += ")\r\n{\r\n" + calfunc + "\r\n}";
//sb.Append("\r\n" + str);
sb.Append("\r\n" + summary + "\r\n" + str + "\r\n\r\n\r\n");
}
}
}
var str1 = sb.ToString();
Console.WriteLine(str1);
Console.Read();
}
/// <summary>
/// 生成某个类的接口方法及注释
/// </summary>
/// <param name="moduleName2">模块名称</param>
/// <param name="className2">类名</param>
public static void ExtractInterfaceMthod2(string moduleName2,string className2)
{
var types = Assembly.Load(moduleName2).GetTypes();
StringBuilder sb = new StringBuilder();
string str = "";
string calfunc = "";
Dictionary<string, ClassSummary> classSummaryDictionary = new Dictionary<string, ClassSummary>();
XmlDocument xmlDoc = new XmlDocument();
string configPath = AppDomain.CurrentDomain.BaseDirectory + $@"{moduleName2}.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 fullName = member.Attributes.GetNamedItem("name").InnerText.ToString();
string left = fullName.Split('(')[0];
string className = left.Split('.')[left.Split('.').Length - 2];
string methodName = left.Split('.')[left.Split('.').Length - 1];
if (!classSummaryDictionary.ContainsKey(className))
{
classSummaryDictionary[className] = new ClassSummary();
}
ClassSummary cs = classSummaryDictionary[className];
string[] m2arr = fullName.Split(new string[] { methodName }, StringSplitOptions.None);
methodName = methodName + m2arr[1];
methodName = methodName.Replace("System.", "").Replace("@", "");
if (!cs.MethodSummaryDictionary.ContainsKey(methodName))
{
//GetCouponBondValue(Double[], Double[], Int32, Double, String, Double, DateTime, DateTime, DateTime, Int32, Int32, Boolean, Double)
cs.MethodSummaryDictionary[methodName] = new MethodSummary();
}
MethodSummary methodSummary = cs.MethodSummaryDictionary[methodName];
methodSummary.MethodZhushi = oriContent;
XmlNodeList paramNodeList = member.SelectNodes("param");
foreach (XmlNode paramNode in paramNodeList)
{
string parameterName = paramNode.Attributes.GetNamedItem("name").InnerText;
string paramSummary = paramNode.InnerText;
methodSummary.ParameterSummaryDictonary[parameterName] = paramSummary;
}
}
foreach (var item in types)
{
if (item.Name ==className2)
{
//载入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;
string direction = "";
if (parameter.ParameterType.IsByRef)
{
direction = "ref";
}
if (parameter.IsOut)
{
direction = "out";
}
if (ptype.EndsWith("&"))
{
isout = true;
ptype = $"{direction} {ptype.TrimEnd('&')}";
}
str += ptype + " " + pn + ",";
if (isout)
{
pn = $"{direction} " + pn;
}
calfunc += pn + ",";
}
str = str.TrimEnd(',');
string isreturn = returntype != "void" ? "return" : "";
calfunc = calfunc.TrimEnd(',') + ");";
str += ");";
char[] def = m.GetBaseDefinition().ToString().Trim().ToCharArray();
bool start = false;
bool first = true;
foreach (var c in def)
{
if(c==' '&&first)
{
start = true;
methodName = "";
first = false;
continue;
}
if (start)
{
methodName += c.ToString();
}
}
methodName = methodName.Replace("System.", "").Replace(" ByRef","");
methodName = methodName.Replace(" ", "");
string summary = "";
try
{
summary = "/// <summary>\r\n" + "/// " + classSummaryDictionary[item.Name].MethodSummaryDictionary[methodName].MethodZhushi + "\r\n" + "///</summary>";
foreach (var p in classSummaryDictionary[item.Name].MethodSummaryDictionary[methodName].ParameterSummaryDictonary)
{
summary += "\r\n/// <param name=" + "\"" + p.Key + "\"" + ">" + p.Value + " </param>";
}
}
catch (Exception ex)
{
summary = "/// <summary>\r\n" + "/// " + $"Badly formed XML comment ignored for member \"M: ValuationLib.ValuationCal.GetCouponBondValue(System.Double[], System.Double[], System.Int32, System.Double, System.String, System.Double, System.DateTime, System.DateTime, System.DateTime, System.Int32, System.Int32, System.Boolean, System.Double)" + "\r\n" + "///</summary>"; ;
}
sb.Append("\r\n" + summary + "\r\n" + str+"\r\n\r\n\r\n");
}
}
}
var str1 = sb.ToString();
Console.WriteLine(str1);
Console.Read();
}
}
/// <summary>
/// 方法注释
/// </summary>
public class MethodSummary
{
/// <summary>
/// 方法注释构造函数
/// </summary>
public MethodSummary()
{
ParameterSummaryDictonary = new Dictionary<string, string>();
}
/// <summary>
/// 方法名称
/// </summary>
public string MethodName { get; set; }
/// <summary>
/// 方法注释
/// </summary>
public string MethodZhushi { get; set; }
/// <summary>
/// 1个方法对应N个参数的注释
/// </summary>
public Dictionary<string, string> ParameterSummaryDictonary { get; set; }
}
/// <summary>
/// 类注释辅助类
/// </summary>
public class ClassSummary
{
/// <summary>
/// 类注释的构造函数
/// </summary>
public ClassSummary()
{
MethodSummaryDictionary = new Dictionary<string, MethodSummary>();
}
/// <summary>
/// 类名
/// </summary>
public string ClassName { get; set; }
/// <summary>
/// 每个类有N个方法 key:方法名 value:方法注释
/// </summary>
public Dictionary<string, MethodSummary> MethodSummaryDictionary { get; set; }
}
}