My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
namespace yab

open System.Reflection
open System
open System.Collections.Generic
open TypeHelper

module Build =
type AType = { Type : Type; Name : string }

let returning t f = f(t); t
let andApply f t = returning t f
let log t = returning t (fun t -> printfn "Creating %s" <| t.ToString())

let getWriteableProperties t = t.GetType().GetProperties() |> Array.filter (fun p -> p.CanWrite)

let invokeWith f (aConstructor:ConstructorInfo) = f(aConstructor) |> aConstructor.Invoke

let (|AString|AnInt|ADouble|ABoolean|ADateTime|AClass|UnknownType|) (propertyType:Type) =
if propertyType |> is<String> then AString
elif propertyType |> isNullable<Int32> then AnInt
elif propertyType |> isNullable<Double> then ADouble
elif propertyType |> isNullable<Boolean> then ABoolean
elif propertyType |> isNullable<DateTime> then ADateTime
elif propertyType.IsClass then AClass
else UnknownType

let getParameters (methodBase:MethodBase) = methodBase.GetParameters()

let rec valueFor (property:AType) : obj =
match property.Type with
| AString -> box property.Name
| AnInt -> box 0
| ADouble -> box 0.0
| ABoolean -> box false
| ADateTime -> box DateTime.MinValue
| AClass -> box <| build property.Type
| _ -> box <| new obj()
and build = getShortestConstructor >>
invokeWith (getParameters >> Array.map (fun p -> valueFor { Type = p.ParameterType; Name = p.Name })) >>
andApply (fun t -> getWriteableProperties t |> Array.iter(setValueOn t))
and setValueOn t p = p.SetValue(t, valueFor {Type = p.PropertyType; Name = p.Name}, null)

type Builder<'a>() =
let withs =new List<Action<'a>>()

member this.With(f) =
returning this (fun _ ->
withs.Add(f))

member this.Build() =
returning (build typeof<'a> :?> 'a) (fun t ->
Seq.iter (fun (s:Action<'a>) -> s.Invoke(t)) withs)

let A<'a> = Builder<'a>()

Change log

d433f42ad29a by mark on May 5, 2010   Diff
mark: more files to ignore & handling
multiple withs
Go to: 
Project members, sign in to write a code review

Older revisions

491fc184df2c by mark on May 4, 2010   Diff
mark adding in build file
5e23a8090c27 by mark on May 3, 2010   Diff
mark: initial checkin for yab
All revisions of this file

File info

Size: 2398 bytes, 55 lines
Powered by Google Project Hosting