lundi 5 septembre 2016

FSharp Reflection in fsx script - does the module define a variable of name foo?

If I have a module of name MyModule defined in an .fsx script and referenced from within another .fsx script; is it possible to determine at runtime if the module defines a variable foo?

I am trying to implement something like this:

let fooWithDefault = 
    let cfgType:Type = typedefof<MyModule>
    let propOpt = 
        cfgType.GetProperties()
        |> Seq.tryFind( fun p -> p.Name = "foo")

    match propOpt with
    | Some foo -> foo.GetValue(null).ToString()
    | None -> "My Default Value for f"

The above attempt fails with the error:

The type 'MyModule' is not defined





Aucun commentaire:

Enregistrer un commentaire