100 lines
3.5 KiB
C#
100 lines
3.5 KiB
C#
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 "";
|
||
}
|
||
|
||
}
|
||
}
|