需要将帮助C#转换为F吗#
我在课下写F#需要将帮助C#转换为F吗#,c#,f#,C#,F#,我在课下写F# 使用System.ComponentModel; 运用系统反思; 名称空间系统 { 公共静态类枚举扩展 { { FieldInfo fi=value.GetType().GetField(value.ToString()); DescriptionAttribute[]属性= (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),false; 返回属性。长度>0?属性[0]。说明:v
使用System.ComponentModel;
运用系统反思;
名称空间系统
{
公共静态类枚举扩展
{
{
FieldInfo fi=value.GetType().GetField(value.ToString());
DescriptionAttribute[]属性=
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),false;
返回属性。长度>0?属性[0]。说明:value.ToString();
}
公共静态字符串描述(此TEnum枚举值),其中TEnum:struct
{
返回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()
你能分享你到目前为止得到的F#版本,并描述你到底停留在什么位置吗?静态成员描述(enumValue:seq)=EnumExtensions.GetEnumDescription(Enum.ToObject(Object)(enumValue))我停留在最后一行,除了所有完成都无法在Enum typesTry中获得返回值这是为了将扩展方法添加到F#
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