0
votes

I am pulling in some XML data using XmlProvider, and I will be accessing it from C#. As you can't use type provided fields directly from C#, I need create record out of them. I can do this by hand but I believe this should be possible to automate using reflection. If I create record types with the same names and types as the fields in the type provider, I should be able to use something like FSharpValue.MakeRecord(typeof<MyType>,values) where values is an array of objects.

What I don't know is how to get the array of values out of the type provider, and how to handle nested records, for instance:

    type Address =
    {
        Address1     : string
        City         : string
        State        : string
    }

    type Client =
    {
        Id        : int            
        FullName  : string
        Address   : Address
    }

In this case Client contains one Address. Will I need to walk the tree and use MakeRecord on the leaves and work my way up?

1
That is a good point. I was imagining I could use the type provider to do logic on the F# side before handing off the record to C#, but if I have to build up the records by hand or reflection anyway, the type provider does become redundant. - jackmott
Yes, I will be happy to accept it - jackmott

1 Answers

5
votes

If you're willing to hand code the types, why do you need the type provider in the first place?

If you're doing some additional logic on F# side, you'll have no choice but to create the records manually anyway. And if you're not doing anything, you can just use the .NET out of the box serializer (or another library) to create them from xml.