gpt4 book ai didi

enums - F# 中枚举的注释

转载 作者:行者123 更新时间:2023-12-04 10:35:39 25 4
gpt4 key购买 nike

我在 F# 中有一个枚举:

type Gender = Undisclosed = 0 | Male = 1 | Female = 2

等效的 C# 代码将是
public enum Gender
{
Undisclosed,
Male,
Female
}

事实上,在C#中,我可以更上一层楼。在 cshtml 的下拉菜单中使用性别页面,我可以这样做:
public enum Gender
{
[Display(ResourceType = typeof(LocalisedStrings), Name = "GenderUndisclosed")] Undisclosed,
[Display(ResourceType = typeof(LocalisedStrings), Name = "GenderMale")] Male,
[Display(ResourceType = typeof(LocalisedStrings), Name = "GenderFemale")] Female
}

不幸的是,如果我尝试向 F# 枚举成员添加类似的注释,F# 编译器会说“此处不允许使用属性”。有没有解决的办法?如果可能的话,我想避免创建重复的类并执行 Automapper voodoo。

最佳答案

您需要一个 |在属性之前。

// Doesn't compile. "Attributes are not allowed here"
type Foo = [<Bar>] Baz = 0

// Compiles.
type Foo = | [<Bar>] Baz = 0

在你的情况下,这将是:
type Gender = 
| [<Display(ResourceType = typeof<LocalisedStrings>, Name = "GenderUndisclosed")>] Undisclosed = 0
| [<Display(ResourceType = typeof<LocalisedStrings>, Name = "GenderMale")>] Male = 1
| [<Display(ResourceType = typeof<LocalisedStrings>, Name = "GenderFemale")>] Female = 2

关于enums - F# 中枚举的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com