3 people like it.
Like the snippet!
Dynamic opearator
Get/put field/property and invoke method (one and multi arguments) via "?" operator.
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:
56:
57:
58:
59:
|
// Dynamic libray for F#.
// Syntax: obj?FieldOrProperty() => obj
// obj?Property1?SubProperty2?MethodName1(arg)?MethodName2()?FieldOrProperty() => obj
// obj?MethodName(argument1,argement2,...) => obj or null
// obj?FieldOrPropertyName<-Value => unit
open System
open System.Reflection
open System.Text.RegularExpressions
let rec (?) (ob:obj) name=
let (|Tuple|_|) o=
Regex.Match(o.GetType().Name,"^Tuple`(\d*)")|>function
|m when m.Success->{1..m.Groups.[1].Value|>int}
|>Seq.map (fun x->((?) o ("Item"+string(x))(null)))
|>Seq.toArray|>Some
|_->None
let x=ob|>function
| :? (obj->obj) as x->x()
|x->x
let tp=x.GetType()
(fun (arg:obj) ->let ar=arg|>function
|null->[||]
|Tuple(arr)->arr
|_->[|arg|]
let fn2 bn=tp.InvokeMember(name,bn,null,x,ar)
try fn2 BindingFlags.GetProperty
with _ ->try fn2 BindingFlags.GetField
with _ -> fn2 BindingFlags.InvokeMethod)
let (?<-) (ob:obj) name arg=
let x=ob|>function
| :? (obj->obj) as x->x()
|x->x
let fn bn=x.GetType().InvokeMember(name,bn,null,x,[|arg|])|>ignore
try fn BindingFlags.SetProperty
with _ -> fn BindingFlags.SetField
// Example 1
//open System.Windows.Forms
//let f=new Form():>obj
//let tb=new TextBox():>obj
//let x=125:>obj
//x?MinValue() // !!!! Warning adding "()" symbols after last field or property
//f?Show()
//f?SetDesktopLocation(100,200)
//tb?Parent<-f
//f?Controls?Item(0)?Text<-"Check"
//tb?Text()
// Example 2
//let excel=Activator.CreateInstance(Type.GetTypeFromProgID("excel.application"))
//excel?Visible<-true
//let wb=excel?Workbooks?Add()
//let sheet=wb?Worksheets?Item(1)
//sheet?Range("a1")?Value<-3
//sheet?Range("b1")?Value<-4
//sheet?Range("c1")?Value<-"=a1+b1"
//sheet?Range("c1")?Text()
//excel?Quit()
|
namespace System
namespace System.Reflection
namespace System.Text
namespace System.Text.RegularExpressions
val ob : obj
type obj = Object
Full name: Microsoft.FSharp.Core.obj
val name : string
Multiple items
type Tuple =
static member Create<'T1> : item1:'T1 -> Tuple<'T1> + 7 overloads
Full name: System.Tuple
--------------------
type Tuple<'T1> =
new : item1:'T1 -> Tuple<'T1>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member ToString : unit -> string
Full name: System.Tuple<_>
--------------------
type Tuple<'T1,'T2> =
new : item1:'T1 * item2:'T2 -> Tuple<'T1, 'T2>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member ToString : unit -> string
Full name: System.Tuple<_,_>
--------------------
type Tuple<'T1,'T2,'T3> =
new : item1:'T1 * item2:'T2 * item3:'T3 -> Tuple<'T1, 'T2, 'T3>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member ToString : unit -> string
Full name: System.Tuple<_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 -> Tuple<'T1, 'T2, 'T3, 'T4>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member ToString : unit -> string
Full name: System.Tuple<_,_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4,'T5> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 * item5:'T5 -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member Item5 : 'T5
member ToString : unit -> string
Full name: System.Tuple<_,_,_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 * item5:'T5 * item6:'T6 -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member Item5 : 'T5
member Item6 : 'T6
member ToString : unit -> string
Full name: System.Tuple<_,_,_,_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6,'T7> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 * item5:'T5 * item6:'T6 * item7:'T7 -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member Item5 : 'T5
member Item6 : 'T6
member Item7 : 'T7
...
Full name: System.Tuple<_,_,_,_,_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'TRest> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 * item5:'T5 * item6:'T6 * item7:'T7 * rest:'TRest -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'TRest>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member Item5 : 'T5
member Item6 : 'T6
member Item7 : 'T7
...
Full name: System.Tuple<_,_,_,_,_,_,_,_>
--------------------
Tuple(item1: 'T1) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4, item5: 'T5) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4, item5: 'T5, item6: 'T6) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4, item5: 'T5, item6: 'T6, item7: 'T7) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4, item5: 'T5, item6: 'T6, item7: 'T7, rest: 'TRest) : unit
val o : 'a
Multiple items
type Regex =
new : pattern:string -> Regex + 1 overload
member GetGroupNames : unit -> string[]
member GetGroupNumbers : unit -> int[]
member GroupNameFromNumber : i:int -> string
member GroupNumberFromName : name:string -> int
member IsMatch : input:string -> bool + 1 overload
member Match : input:string -> Match + 2 overloads
member Matches : input:string -> MatchCollection + 1 overload
member Options : RegexOptions
member Replace : input:string * replacement:string -> string + 5 overloads
...
Full name: System.Text.RegularExpressions.Regex
--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
Regex.Match(input: string, pattern: string) : Match
Regex.Match(input: string, pattern: string, options: RegexOptions) : Match
Object.GetType() : Type
val m : Match
property Group.Success: bool
property Match.Groups: GroupCollection
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
module Seq
from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map
val x : int
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = String
Full name: Microsoft.FSharp.Core.string
val toArray : source:seq<'T> -> 'T []
Full name: Microsoft.FSharp.Collections.Seq.toArray
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val x : obj
val x : (obj -> obj)
val tp : Type
val arg : obj
val ar : obj []
Multiple items
active recognizer Tuple: 'a -> obj [] option
--------------------
type Tuple =
static member Create<'T1> : item1:'T1 -> Tuple<'T1> + 7 overloads
Full name: System.Tuple
--------------------
type Tuple<'T1> =
new : item1:'T1 -> Tuple<'T1>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member ToString : unit -> string
Full name: System.Tuple<_>
--------------------
type Tuple<'T1,'T2> =
new : item1:'T1 * item2:'T2 -> Tuple<'T1, 'T2>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member ToString : unit -> string
Full name: System.Tuple<_,_>
--------------------
type Tuple<'T1,'T2,'T3> =
new : item1:'T1 * item2:'T2 * item3:'T3 -> Tuple<'T1, 'T2, 'T3>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member ToString : unit -> string
Full name: System.Tuple<_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 -> Tuple<'T1, 'T2, 'T3, 'T4>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member ToString : unit -> string
Full name: System.Tuple<_,_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4,'T5> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 * item5:'T5 -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member Item5 : 'T5
member ToString : unit -> string
Full name: System.Tuple<_,_,_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 * item5:'T5 * item6:'T6 -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member Item5 : 'T5
member Item6 : 'T6
member ToString : unit -> string
Full name: System.Tuple<_,_,_,_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6,'T7> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 * item5:'T5 * item6:'T6 * item7:'T7 -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member Item5 : 'T5
member Item6 : 'T6
member Item7 : 'T7
...
Full name: System.Tuple<_,_,_,_,_,_,_>
--------------------
type Tuple<'T1,'T2,'T3,'T4,'T5,'T6,'T7,'TRest> =
new : item1:'T1 * item2:'T2 * item3:'T3 * item4:'T4 * item5:'T5 * item6:'T6 * item7:'T7 * rest:'TRest -> Tuple<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'TRest>
member Equals : obj:obj -> bool
member GetHashCode : unit -> int
member Item1 : 'T1
member Item2 : 'T2
member Item3 : 'T3
member Item4 : 'T4
member Item5 : 'T5
member Item6 : 'T6
member Item7 : 'T7
...
Full name: System.Tuple<_,_,_,_,_,_,_,_>
--------------------
Tuple(item1: 'T1) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4, item5: 'T5) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4, item5: 'T5, item6: 'T6) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4, item5: 'T5, item6: 'T6, item7: 'T7) : unit
--------------------
Tuple(item1: 'T1, item2: 'T2, item3: 'T3, item4: 'T4, item5: 'T5, item6: 'T6, item7: 'T7, rest: 'TRest) : unit
val arr : obj []
val fn2 : (BindingFlags -> obj)
val bn : BindingFlags
Type.InvokeMember(name: string, invokeAttr: BindingFlags, binder: Binder, target: obj, args: obj []) : obj
Type.InvokeMember(name: string, invokeAttr: BindingFlags, binder: Binder, target: obj, args: obj [], culture: Globalization.CultureInfo) : obj
Type.InvokeMember(name: string, invokeAttr: BindingFlags, binder: Binder, target: obj, args: obj [], modifiers: ParameterModifier [], culture: Globalization.CultureInfo, namedParameters: string []) : obj
type BindingFlags =
| Default = 0
| IgnoreCase = 1
| DeclaredOnly = 2
| Instance = 4
| Static = 8
| Public = 16
| NonPublic = 32
| FlattenHierarchy = 64
| InvokeMethod = 256
| CreateInstance = 512
...
Full name: System.Reflection.BindingFlags
field BindingFlags.GetProperty = 4096
field BindingFlags.GetField = 1024
field BindingFlags.InvokeMethod = 256
val arg : 'a
val fn : (BindingFlags -> unit)
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
field BindingFlags.SetProperty = 8192
field BindingFlags.SetField = 2048
More information