需要将帮助 C# 转换为 F#

Need Converting Help C# to F#

我在下面写 class 到 F#

using System.ComponentModel;
using System.Reflection;
namespace System
{
    public static class EnumExtensions
    {
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = 
            (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }
        public static string Description<TEnum>(this TEnum enumValue) where TEnum : struct
        {
            return GetEnumDescription((Enum)(object)(enumValue));
        }
    }
}

但是我卡住了有人可以帮助我吗

像这样应该可以解决问题:

let getEnumDescription value = 
    let fi = 
        value.GetType().GetField(value.ToString())
    
    let attributes = 
        fi.GetCustomAttributes(typedefof<DescriptionAttribute>, false)
        |> Array.map (fun x -> x :?> DescriptionAttribute)

    if attributes.Length > 0 then
        attributes.[0].Description
    else 
        value.ToString()

然后你可以这样称呼它:

let red = Color.Red |> getEnumDescription
let blue = Color.Blue |> getEnumDescription

printfn "Red is %s\nBlue is %s" red blue

//Red is Red
//Blue is Deep Blue