5 people like it.
Like the snippet!
reflection
reflection, not exactly efficient but good canvas to start exploring
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:
|
open System.Reflection
open System.Reflection.Emit
//type generic (x) = member this.funct = 1
let opcodes =
typeof<OpCodes>.GetFields()
|> Seq.map (fun x -> x.GetValue() :?> OpCode |> fun o -> o.Value, o.Name)
|> Map.ofSeq
let find_opcode (b : byte array) =
Seq.map (fun bs -> Map.tryFind (int16 bs) opcodes) b
let getMSIL F =
let t1 = F.GetType().GetFields(BindingFlags.NonPublic ||| BindingFlags.Instance)
|> Array.map (fun field -> field.Name)
t1.GetType().GetMethods()
|> Seq.map (fun mb -> try mb.GetMethodBody().GetILAsByteArray() with ex -> [|0uy|])
|> Seq.map find_opcode
|> Seq.concat
|> Seq.filter ((<>) None) //lol
let printMSIL F =
getMSIL F |> Seq.iter (fun x-> printfn "%s" x.Value)
//prints about 1200 loc msil? surely that isnt right for something like let x = 1 printMSIL x, guess its printing whole assembly?
|
namespace System
namespace System.Reflection
namespace System.Reflection.Emit
val opcodes : Map<int16,string>
Full name: Script.opcodes
val typeof<'T> : System.Type
Full name: Microsoft.FSharp.Core.Operators.typeof
type OpCodes =
static val Nop : OpCode
static val Break : OpCode
static val Ldarg_0 : OpCode
static val Ldarg_1 : OpCode
static val Ldarg_2 : OpCode
static val Ldarg_3 : OpCode
static val Ldloc_0 : OpCode
static val Ldloc_1 : OpCode
static val Ldloc_2 : OpCode
static val Ldloc_3 : OpCode
...
Full name: System.Reflection.Emit.OpCodes
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 : FieldInfo
FieldInfo.GetValue(obj: obj) : obj
type OpCode =
struct
member Equals : obj:obj -> bool + 1 overload
member FlowControl : FlowControl
member GetHashCode : unit -> int
member Name : string
member OpCodeType : OpCodeType
member OperandType : OperandType
member Size : int
member StackBehaviourPop : StackBehaviour
member StackBehaviourPush : StackBehaviour
member ToString : unit -> string
...
end
Full name: System.Reflection.Emit.OpCode
val o : OpCode
property OpCode.Value: int16
property OpCode.Name: string
Multiple items
module Map
from Microsoft.FSharp.Collections
--------------------
type Map<'Key,'Value (requires comparison)> =
interface IEnumerable
interface IComparable
interface IEnumerable<KeyValuePair<'Key,'Value>>
interface ICollection<KeyValuePair<'Key,'Value>>
interface IDictionary<'Key,'Value>
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
member Add : key:'Key * value:'Value -> Map<'Key,'Value>
member ContainsKey : key:'Key -> bool
override Equals : obj -> bool
member Remove : key:'Key -> Map<'Key,'Value>
...
Full name: Microsoft.FSharp.Collections.Map<_,_>
--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val ofSeq : elements:seq<'Key * 'T> -> Map<'Key,'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Map.ofSeq
val find_opcode : b:byte array -> seq<string option>
Full name: Script.find_opcode
val b : byte array
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.byte
--------------------
type byte = System.Byte
Full name: Microsoft.FSharp.Core.byte
type 'T array = 'T []
Full name: Microsoft.FSharp.Core.array<_>
val bs : byte
val tryFind : key:'Key -> table:Map<'Key,'T> -> 'T option (requires comparison)
Full name: Microsoft.FSharp.Collections.Map.tryFind
Multiple items
val int16 : value:'T -> int16 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int16
--------------------
type int16 = System.Int16
Full name: Microsoft.FSharp.Core.int16
--------------------
type int16<'Measure> = int16
Full name: Microsoft.FSharp.Core.int16<_>
val getMSIL : F:'a -> seq<string option>
Full name: Script.getMSIL
val F : 'a
val t1 : string []
System.Object.GetType() : System.Type
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.NonPublic = 32
field BindingFlags.Instance = 4
module Array
from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.map
val field : FieldInfo
property MemberInfo.Name: string
val mb : MethodInfo
MethodBase.GetMethodBody() : MethodBody
val ex : exn
val concat : sources:seq<#seq<'T>> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.concat
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.filter
union case Option.None: Option<'T>
val printMSIL : F:'a -> unit
Full name: Script.printMSIL
val iter : action:('T -> unit) -> source:seq<'T> -> unit
Full name: Microsoft.FSharp.Collections.Seq.iter
val x : string option
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property Option.Value: string
More information