Hi,

F# represents fields as properties and the attribute is attached to the property not the field. You just need to call the GetProperties instead of GetFields method:

1
let fields = (typeof<MyStruct>).GetProperties(BindingFlags.Public ||| BindingFlags.NonPublic ||| BindingFlags.Instance)

Cheers,

Rob

By on 10/16/2008 4:23 AM ()

Hi,
just one addition - if you for some reason need to specify the attribute for the field, then you can use the following syntax (I guess it might be useful if you wanted something like specifying layout of the struct):

1
2
3
4
5
6
7
 
[<Struct>]
type MyStruct =
  [<field:MyCustomAttribute("7")>] // Note, the "field" prefix here!
  [<System.Runtime.Serialization.DataMemberAttribute>]
  val field0 : string
  new(s) = {field0 = s}

<i>[Edit: Oops! Brian was faster!]<i> T.

By on 10/16/2008 7:58 AM ()

Hi ! Thank you guys for your answers that was very helpfull (albeit I still don't get why it works with a CustomAttribute class written in C#, anyway...)

Stringer

By on 10/16/2008 1:02 PM ()

Alternatively, tell the attribute you want it on the field, like so

1
[<field:MyCustomAttribute("7")>]
By on 10/16/2008 7:58 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper