现场程序优化

This commit is contained in:
2026-08-12 18:16:50 +08:00
parent b43090e56e
commit 30f5f6f8d6
16 changed files with 1020 additions and 26 deletions
+112 -16
View File
@@ -192,21 +192,26 @@ namespace TestingModule.ViewModels
var assembly = Assembly.LoadFrom(dllPath);
Assemblies.Add(assembly);
// 加载对应的XML注释文件 (项目没有用到)
//string xmlPath = Path.ChangeExtension(dllPath, ".xml");
//if (File.Exists(xmlPath))
//{
// try
// {
// XmlDocument xmlDoc = new XmlDocument();
// xmlDoc.Load(xmlPath);
// _xmlDocumentCache[assembly.FullName!] = xmlDoc;
// }
// catch (Exception xmlEx)
// {
// LoggerHelper.WarnWithNotify($"加载XML注释失败: {Path.GetFileName(xmlPath)} - {xmlEx.Message}");
// }
//}
//加载对应的XML注释文件
string xmlPath = Path.ChangeExtension(dllPath, ".xml");
if (File.Exists(xmlPath))
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlDocumentCache[assembly.FullName!] = xmlDoc;
LoggerHelper.Info($"[XML注释] 成功加载: {Path.GetFileName(xmlPath)} -> 程序集 [{assembly.FullName}]");
}
catch (Exception xmlEx)
{
LoggerHelper.Warn($"加载XML注释失败: {Path.GetFileName(xmlPath)} - {xmlEx.Message}");
}
}
else
{
LoggerHelper.Warn($"[XML注释] XML文件不存在: {xmlPath}");
}
}
catch (Exception ex)
{
@@ -284,6 +289,7 @@ namespace TestingModule.ViewModels
{
Name = subProgram.Name,
Tag = subProgram,
Tooltip = subProgram.FilePath
});
}
@@ -368,6 +374,7 @@ namespace TestingModule.ViewModels
{
Name = $"{method.Name}({paramText})",
Tag = method,
Tooltip = GetMethodDocumentation(method),
};
typeNode.Children.Add(methodNode);
@@ -433,6 +440,95 @@ namespace TestingModule.ViewModels
}
}
}
// 添加获取注释的方法
private string? GetMethodDocumentation(MethodInfo method)
{
if (method.DeclaringType == null) return null;
try
{
string assemblyName = method.DeclaringType.Assembly.FullName!;
if (!_xmlDocumentCache.TryGetValue(assemblyName, out XmlDocument? xmlDoc))
{
LoggerHelper.Warn($"[XML注释] 缓存未命中: 程序集 [{assemblyName}],缓存包含 { _xmlDocumentCache.Count} 个条目: [{string.Join(", ", _xmlDocumentCache.Keys)}]");
return null;
}
// 生成XML文档中的成员ID
string memberName = $"M:{method.DeclaringType.FullName}.{method.Name}";
var parameters = method.GetParameters();
if (parameters.Length > 0)
{
memberName += "(" + string.Join(",", parameters.Select(p => p.ParameterType.FullName)) + ")";
}
// 查找注释节点
XmlNode? memberNode = xmlDoc.SelectSingleNode($"//member[@name='{memberName}']");
if (memberNode == null)
{
LoggerHelper.Warn($"[XML注释] 节点未找到: {memberName}");
return null;
}
// 获取摘要(summary
var summaryNode = memberNode.SelectSingleNode("summary");
string documentation = "";
if (summaryNode != null)
{
documentation += CleanXmlContent(summaryNode.InnerXml);
}
// 获取参数注释(param
var paramNodes = memberNode.SelectNodes("param");
if (paramNodes != null && paramNodes.Count > 0)
{
documentation += "\n\n参数:";
foreach (XmlNode paramNode in paramNodes)
{
string? paramName = paramNode.Attributes?["name"]?.Value;
if (!string.IsNullOrEmpty(paramName))
{
documentation += $"\n • {paramName}: {CleanXmlContent(paramNode.InnerXml)}";
}
}
}
// 获取返回值注释(returns
var returnsNode = memberNode.SelectSingleNode("returns");
if (returnsNode != null)
{
documentation += $"\n\n返回值: {CleanXmlContent(returnsNode.InnerXml)}";
}
return string.IsNullOrWhiteSpace(documentation)
? null
: System.Net.WebUtility.HtmlDecode(documentation.Trim());
}
catch (Exception ex)
{
LoggerHelper.Warn($"获取注释失败: {method.Name} - {ex.Message}");
return null;
}
}
// 辅助方法:清理XML内容
private string CleanXmlContent(string xmlContent)
{
return xmlContent
.Replace("<see cref=\"", "")
.Replace("\"/>", "")
.Replace("<para>", "\n")
.Replace("</para>", "")
.Replace("<seealso", "")
.Replace("/>", "")
.Replace("<c>", "") // 处理代码标签
.Replace("</c>", "")
.Replace("<code>", "")
.Replace("</code>", "")
.Trim();
}
#endregion
#region
@@ -515,7 +611,7 @@ namespace TestingModule.ViewModels
var newStep = new StepVM
{
Name = subProgram.Name,
StepType = "子程序"
StepType = "子程序",
};
var jsonstr = File.ReadAllText($"{subProgram.FilePath}");
var tmp = JsonConvert.DeserializeObject<ProgramVM>(jsonstr);