mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-23 09:36:34 +08:00
补充了Library的注释
This commit is contained in:
@@ -6,11 +6,22 @@ using System.Text;
|
||||
|
||||
namespace Serein.Library.Utils
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 枚举工具类,用于枚举转换器
|
||||
/// </summary>
|
||||
public static class EnumHelper
|
||||
{
|
||||
public static bool TryConvertEnum<T>(this string value, out T result) where T : struct, Enum
|
||||
/// <summary>
|
||||
/// 将字符串的字面量枚举值,转为对应的枚举值
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum">枚举</typeparam>
|
||||
/// <param name="value">枚举字面量</param>
|
||||
/// <param name="result">返回的枚举值</param>
|
||||
/// <returns>是否转换成功</returns>
|
||||
public static bool TryConvertEnum<TEnum>(this string value, out TEnum result) where TEnum : struct, Enum
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value) && Enum.TryParse(value, true, out T tempResult) && Enum.IsDefined(typeof(T), tempResult))
|
||||
if (!string.IsNullOrEmpty(value) && Enum.TryParse(value, true, out TEnum tempResult) && Enum.IsDefined(typeof(TEnum), tempResult))
|
||||
{
|
||||
result = tempResult;
|
||||
return true;
|
||||
@@ -18,6 +29,15 @@ namespace Serein.Library.Utils
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从枚举值的 BindValueAttribute 特性中 获取绑定的参数(用于绑定了某些内容的枚举值)
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum">枚举类型</typeparam>
|
||||
/// <typeparam name="TResult">返回类型</typeparam>
|
||||
/// <param name="enumValue">枚举值</param>
|
||||
/// <param name="valueSelector">选择什么参数</param>
|
||||
/// <returns></returns>
|
||||
public static TResult GetBoundValue<TEnum, TResult>(TEnum enumValue, Func<BindValueAttribute, object> valueSelector)
|
||||
where TEnum : Enum
|
||||
{
|
||||
@@ -26,6 +46,7 @@ namespace Serein.Library.Utils
|
||||
|
||||
return attribute != null ? (TResult)valueSelector(attribute) : default;
|
||||
}
|
||||
|
||||
public static object GetBoundValue(Type enumType,object enumValue, Func<BindValueAttribute, object> valueSelector)
|
||||
{
|
||||
var fieldInfo = enumType.GetField(enumValue.ToString());
|
||||
@@ -34,6 +55,16 @@ namespace Serein.Library.Utils
|
||||
return attribute != null ? valueSelector(attribute) : default;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 从枚举值从获取自定义特性的成员,并自动转换类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnum">枚举类型</typeparam>
|
||||
/// <typeparam name="TAttribute">自定义特性类型</typeparam>
|
||||
/// <typeparam name="TResult">返回类型</typeparam>
|
||||
/// <param name="enumValue">枚举值</param>
|
||||
/// <param name="valueSelector">特性成员选择</param>
|
||||
/// <returns></returns>
|
||||
public static TResult GetBoundValue<TEnum, TAttribute, TResult>(TEnum enumValue,
|
||||
Func<TAttribute, TResult> valueSelector)
|
||||
where TEnum : Enum
|
||||
|
||||
Reference in New Issue
Block a user