Annotations for listings in F #

I have an enumeration in F #:

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

Equivalent C # code would be

 public enum Gender { Undisclosed, Male, Female } 

In fact, in C #, I can go one step better. To use the gender in the drop-down list on the cshtml page, I can do this:

 public enum Gender { [Display(ResourceType = typeof(LocalisedStrings), Name = "GenderUndisclosed")] Undisclosed, [Display(ResourceType = typeof(LocalisedStrings), Name = "GenderMale")] Male, [Display(ResourceType = typeof(LocalisedStrings), Name = "GenderFemale")] Female } 

Unfortunately, the F # compiler says that โ€œattributes are not allowed hereโ€ if I try to add similar annotations to the members of the F # enumeration. Is there any way around this? I would like to avoid duplicating the class and running Automapper voodoo if possible.

+6
source share
2 answers

Before the attribute you need | .

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

In your case, this will work out:

 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 
+6
source

This should work:

 type Gender = | [<Display>] Undisclosed = 0 | [<Display>] Male = 1 | [<Display>] Female = 2 
+5
source