Before you leave: though my code is in F#, this question is applicable to any .NET langague.
So here's my situation -- I have a simple ProgramOptions record in F# for holding command line option data. Each field represents a distinct option, and they can have default values, which are marked with a custom attribute.
type ProgramOptionAttribute(defaultValue: obj) =
inherit Attribute()
type ProgramOptions =
{ [<ProgramOption("render.pdf")>] output: string
[<ProgramOption(true)>] printOutput: bool
// several more
}
I've written a nice little function elsewhere to dynamically instantiate this record using the attribute data, given the raw command line options. That works great. But it's really easy to introduce a runtime type mismatch by providing an object to the attribute that is a different type from the field (since there's just a simple cast from obj -> field type later down the line). For example:
// Obviously wrong, but compiles without complaint, failing at runtime
[<ProgramOption(42)>] myField: bool
Is there a way to make that type safe? Some kind of generic trickery? Or is what I want impossible?