I've loaded a C# dll in a Fsi session. Running C# methods returns some C# types. I've written a helper function to explore the Properties for a given C# type.
The program is failing with an error:
stdin(95,21): error FS0039: The type 'RuntimePropertyInfo' is not defined
Is this possible to do? Or am I beating a dead horse?
let getPropertyNames (s : System.Type)=
Seq.map (fun (t:System.Reflection.RuntimePropertyInfo) -> t.Name) (typeof<s>.GetProperties())
typeof<TypeName>.GetProperties() //seems to work.
I'm just aiming for a pretty print of C# fields.
I think I've found a way to do this. And it seems to work. I'm not able to answer myself. So I'll accept the answer of anyone who gives a better example than this.
let getPropertyNames (s : System.Type)=
let properties = s.GetProperties()
properties
|> Array.map (fun x -> x.Name)
|> Array.iter (fun x -> printfn "%s" x)
As mentioned in the comments, you can use System.Reflection.PropertyInfo
in the type annotation. Your code also has typeof<s>
, but s
is already a variable of type System.Type
, so you can just call GetProperties
on s
directly:
let getPropertyNames (s : System.Type)=
Seq.map (fun (t:System.Reflection.PropertyInfo) -> t.Name) (s.GetProperties())
getPropertyNames (typeof<System.String>)
You can also avoid the type annotation altogether by using pipe:
let getPropertyNames (s : System.Type)=
s.GetProperties() |> Seq.map (fun t -> t.Name)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.