添加Enum转换为Option的扩展

This commit is contained in:
Sanchime 2022-05-08 15:43:36 +08:00
parent 01633d1c4c
commit 83cc3df7aa
1 changed files with 12 additions and 0 deletions

View File

@ -0,0 +1,12 @@
using Sanchime.Functional.Core.Products;
namespace Sanchime.Functional.Core.Extensions;
public static class EnumExtension
{
/// <summary>
/// 将字符串转换为相应的枚举值的<see cref="Option"/
/// </summary>
public static Option<T> Parse<T>(this string @this) where T : struct
=> Enum.TryParse(@this, out T val) ? Option.Some(val) : Option.None;
}