升级2.13.0
新增支持从xml文件中获取Type和属性的summary。
This commit is contained in:
parent
0e8b885784
commit
a7617c35e1
|
@ -8,7 +8,7 @@
|
|||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>2.12.0</Version>
|
||||
<Version>2.13.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Falcon.SugarApi
|
||||
{
|
||||
|
@ -17,7 +20,7 @@ namespace Falcon.SugarApi
|
|||
/// <param name="info">属性</param>
|
||||
/// <param name="p">定义的特性</param>
|
||||
/// <returns>定义返回True,否则False</returns>
|
||||
public static bool TryGetAttribute<T>([NotNull] this PropertyInfo info, out T p) where T : Attribute {
|
||||
public static bool TryGetAttribute<T>([NotNull] this PropertyInfo info,out T p) where T : Attribute {
|
||||
p = info.GetCustomAttribute<T>();
|
||||
return p != null;
|
||||
}
|
||||
|
@ -29,9 +32,68 @@ namespace Falcon.SugarApi
|
|||
/// <param name="info">属性</param>
|
||||
/// <param name="p">定义的特性</param>
|
||||
/// <returns>定义返回True,否则False</returns>
|
||||
public static bool TryGetAttribute<T>([NotNull] this Type info, out T p) where T : Attribute {
|
||||
public static bool TryGetAttribute<T>([NotNull] this Type info,out T p) where T : Attribute {
|
||||
p = info.GetCustomAttribute<T>();
|
||||
return p != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取类型的summary说明
|
||||
/// </summary>
|
||||
/// <param name="type">类型</param>
|
||||
/// <returns>summary值,没有返回空字符串</returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static string? GetTypeSummary(this Type type) {
|
||||
_ = type ?? throw new ArgumentNullException(nameof(type));
|
||||
var assemblyName = type.Assembly.FullName;
|
||||
if(assemblyName == null) {
|
||||
return "";
|
||||
}
|
||||
var dllLocal = type.Assembly.Location;
|
||||
var xmlLocal = $"{dllLocal.Substring(0,dllLocal.Length - 3)}xml";
|
||||
if(!File.Exists(xmlLocal)) {
|
||||
return "";
|
||||
}
|
||||
var xdoc = XDocument.Load(xmlLocal);
|
||||
if(xdoc == null) {
|
||||
return "";
|
||||
}
|
||||
var members = xdoc.Descendants("member");
|
||||
var find = members.Where(a => a.Attribute("name")?.Value == $"T:{type.FullName}");
|
||||
if(find.Any()) {
|
||||
return find.First().Element("summary")?.Value?.Trim() ?? "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取属性的summary说明
|
||||
/// </summary>
|
||||
/// <param name="property">属性</param>
|
||||
/// <returns>summary值,没有返回空字符串</returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static string GetPropertySummary(this PropertyInfo property) {
|
||||
_ = property ?? throw new ArgumentNullException(nameof(property));
|
||||
var type = property.ReflectedType;
|
||||
if(type == null) {
|
||||
return "";
|
||||
}
|
||||
var dllLocal = type.Assembly.Location;
|
||||
var xmlLocal = $"{dllLocal.Substring(0,dllLocal.Length - 3)}xml";
|
||||
if(!File.Exists(xmlLocal)) {
|
||||
return "";
|
||||
}
|
||||
var xdoc = XDocument.Load(xmlLocal);
|
||||
if(xdoc == null) {
|
||||
return "";
|
||||
}
|
||||
var members = xdoc.Descendants("member");
|
||||
var find = members.Where(a => a.Attribute("name")?.Value == $"P:{type.FullName}.{property.Name}");
|
||||
if(find.Any()) {
|
||||
return find.First().Element("summary")?.Value?.Trim() ?? "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user