[英]Need Converting Help C# to F#
I am writing below class in 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));
}
}
}
But I am stuck can someone help me但我被困住了,有人可以帮我吗
Something like this should do the trick:这样的事情应该可以解决问题:
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()
Then you can call it like so:然后你可以这样称呼它:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.