3 people like it.
Like the snippet!
Showing generic record array from server to html client
Including generic abstraction to manage a RPC call returning either an error or an array of record types from DB server to client browser.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
|
open Microsoft.FSharp.Reflection
let recordHeader<'record>
: string [] =
FSharpType.GetRecordFields(typeof<'record>)
|> Array.map (fun field -> field.Name )
let recordValues<'record> (record:'record)
: obj [] =
FSharpType.GetRecordFields(record.GetType())
|> Array.map (fun field -> FSharpValue.GetRecordField(record,field) )
let toGenericResult<'record> (result: Result<'record[], string>)
: Model.GenericExtract =
match result with
| Error error -> Error error
| Ok array ->
Ok {
Model.Header = recordHeader<'record>;
Model.Values = array |> Array.map recordValues;
}
|
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Reflection
val recordHeader<'record> : string []
Full name: Script.recordHeader
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
type FSharpType =
static member GetExceptionFields : exceptionType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
static member GetFunctionElements : functionType:Type -> Type * Type
static member GetRecordFields : recordType:Type * ?bindingFlags:BindingFlags -> PropertyInfo []
static member GetTupleElements : tupleType:Type -> Type []
static member GetUnionCases : unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo []
static member IsExceptionRepresentation : exceptionType:Type * ?bindingFlags:BindingFlags -> bool
static member IsFunction : typ:Type -> bool
static member IsModule : typ:Type -> bool
static member IsRecord : typ:Type * ?bindingFlags:BindingFlags -> bool
static member IsTuple : typ:Type -> bool
...
Full name: Microsoft.FSharp.Reflection.FSharpType
static member FSharpType.GetRecordFields : recordType:System.Type * ?allowAccessToPrivateRepresentation:bool -> System.Reflection.PropertyInfo []
static member FSharpType.GetRecordFields : recordType:System.Type * ?bindingFlags:System.Reflection.BindingFlags -> System.Reflection.PropertyInfo []
val typeof<'T> : System.Type
Full name: Microsoft.FSharp.Core.Operators.typeof
module Array
from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.map
val field : System.Reflection.PropertyInfo
property System.Reflection.MemberInfo.Name: string
val recordValues : record:'record -> obj []
Full name: Script.recordValues
val record : 'record
type obj = System.Object
Full name: Microsoft.FSharp.Core.obj
System.Object.GetType() : System.Type
type FSharpValue =
static member GetExceptionFields : exn:obj * ?bindingFlags:BindingFlags -> obj []
static member GetRecordField : record:obj * info:PropertyInfo -> obj
static member GetRecordFields : record:obj * ?bindingFlags:BindingFlags -> obj []
static member GetTupleField : tuple:obj * index:int -> obj
static member GetTupleFields : tuple:obj -> obj []
static member GetUnionFields : value:obj * unionType:Type * ?bindingFlags:BindingFlags -> UnionCaseInfo * obj []
static member MakeFunction : functionType:Type * implementation:(obj -> obj) -> obj
static member MakeRecord : recordType:Type * values:obj [] * ?bindingFlags:BindingFlags -> obj
static member MakeTuple : tupleElements:obj [] * tupleType:Type -> obj
static member MakeUnion : unionCase:UnionCaseInfo * args:obj [] * ?bindingFlags:BindingFlags -> obj
...
Full name: Microsoft.FSharp.Reflection.FSharpValue
static member FSharpValue.GetRecordField : record:obj * info:System.Reflection.PropertyInfo -> obj
val toGenericResult<'record> : result:obj -> obj
Full name: Script.toGenericResult
val result : obj
type 'T array = 'T []
Full name: Microsoft.FSharp.Core.array<_>
More information