Falcon.SugarApi/Falcon.SugarApi/TypeExtend.cs
Falcon a7617c35e1 升级2.13.0
新增支持从xml文件中获取Type和属性的summary。
2025-02-12 15:54:57 +08:00

100 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
namespace Falcon.SugarApi
{
/// <summary>
/// 类型相关扩展
/// </summary>
public static class TypeExtend
{
/// <summary>
/// 尝试获取Attribute
/// </summary>
/// <typeparam name="T">Attribute类型</typeparam>
/// <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 {
p = info.GetCustomAttribute<T>();
return p != null;
}
/// <summary>
/// 尝试获取Attribute
/// </summary>
/// <typeparam name="T">Attribute类型</typeparam>
/// <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 {
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 "";
}
}
}