Skip to content

Instantly share code, notes, and snippets.

@kevmal
Last active April 20, 2023 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevmal/640a19408393f0e517ab09d809079f4f to your computer and use it in GitHub Desktop.
Save kevmal/640a19408393f0e517ab09d809079f4f to your computer and use it in GitHub Desktop.
Expr for Option<ProvidedTypeDef>.Value
#r "nuget:Qit,0.0.2-alpha2"
open System
open Qit.ProviderImplementation.ProvidedTypes
open System.Reflection
type CtorFml(tpdef : Type, genericMethodDefinition: ConstructorInfo, parameters: Type[]) =
inherit ConstructorInfo()
let gps = tpdef.GetGenericArguments()
let convParam (p:ParameterInfo) =
{ new ParameterInfo() with
override __.Name = p.Name
override __.ParameterType =
match gps |> Array.tryFindIndex (fun g -> p.ParameterType = g) with
| Some i -> parameters.[i]
| None -> p.ParameterType
override __.Attributes = p.Attributes
override __.RawDefaultValue = p.RawDefaultValue
override __.GetCustomAttributesData() = p.GetCustomAttributesData()
}
let attrs = genericMethodDefinition.Attributes
let isStatic() = genericMethodDefinition.IsStatic
member __.IsTypeInitializer
with get() = isStatic() && hasFlag attrs MethodAttributes.Private
override __.GetParameters() = genericMethodDefinition.GetParameters() |> Array.map convParam
override __.Attributes = attrs
override __.Name = if isStatic() then ".cctor" else ".ctor"
override __.DeclaringType = ProvidedTypeSymbol(ProvidedTypeSymbolKind.Generic(tpdef), parameters |> Array.toList, defaultTypeBuilder) :> Type
override __.IsDefined(_attributeType, _inherit) = true
override this.Invoke(_invokeAttr, _binder, _parameters, _culture) = notRequired this "Invoke" this.Name
override this.Invoke(_obj, _invokeAttr, _binder, _parameters, _culture) = notRequired this "Invoke" this.Name
override this.ReflectedType = notRequired this "ReflectedType" this.Name
override this.GetMethodImplementationFlags() = notRequired this "GetMethodImplementationFlags" this.Name
override this.MethodHandle = notRequired this "MethodHandle" this.Name
override __.GetCustomAttributes(_inherit) = emptyAttributes
override __.GetCustomAttributes(attributeType, _inherit) = Attributes.CreateEmpty attributeType
type MethodFml(tpdef : Type, genericMethodDefinition: MethodInfo, parameters: Type[]) =
inherit MethodInfo()
let gps = tpdef.GetGenericArguments()
let convParam (p:ParameterInfo) =
{ new ParameterInfo() with
override __.Name = p.Name
override __.ParameterType =
match gps |> Array.tryFindIndex (fun g -> p.ParameterType = g) with
| Some i -> parameters.[i]
| None -> p.ParameterType
override __.Attributes = p.Attributes
override __.RawDefaultValue = p.RawDefaultValue
override __.GetCustomAttributesData() = p.GetCustomAttributesData()
}
override this.IsGenericMethod =false
override this.GetGenericArguments() = [||]
override __.GetGenericMethodDefinition() = genericMethodDefinition
override __.DeclaringType = ProvidedTypeSymbol(ProvidedTypeSymbolKind.Generic(tpdef), parameters |> Array.toList, defaultTypeBuilder) :> Type //tpdef.MakeGenericType(parameters)
override __.ToString() = "Method " + genericMethodDefinition.Name
override __.Name = genericMethodDefinition.Name
override __.MetadataToken = genericMethodDefinition.MetadataToken
override __.Attributes = genericMethodDefinition.Attributes
override __.CallingConvention = genericMethodDefinition.CallingConvention
override __.MemberType = genericMethodDefinition.MemberType
override this.IsDefined(_attributeType, _inherit): bool = notRequired this "IsDefined" genericMethodDefinition.Name
override __.ReturnType = instType defaultTypeBuilder (parameters, [| |]) genericMethodDefinition.ReturnType
override __.GetParameters() = genericMethodDefinition.GetParameters() |> Array.map convParam
override __.ReturnParameter = genericMethodDefinition.ReturnParameter |> convParam
override this.ReturnTypeCustomAttributes = notRequired this "ReturnTypeCustomAttributes" genericMethodDefinition.Name
override this.GetBaseDefinition() = notRequired this "GetBaseDefinition" genericMethodDefinition.Name
override this.GetMethodImplementationFlags() = notRequired this "GetMethodImplementationFlags" genericMethodDefinition.Name
override this.MethodHandle = notRequired this "MethodHandle" genericMethodDefinition.Name
override this.Invoke(_obj, _invokeAttr, _binder, _parameters, _culture) = notRequired this "Invoke" genericMethodDefinition.Name
override this.ReflectedType = notRequired this "ReflectedType" genericMethodDefinition.Name
override __.GetCustomAttributes(_inherit) = emptyAttributes
override __.GetCustomAttributes(attributeType, _inherit) = Attributes.CreateEmpty attributeType
type PropertyFml(tpdef : Type, p: PropertyInfo, parameters: Type[]) =
inherit PropertyInfo()
override __.GetCustomAttributesData() = p.GetCustomAttributesData()
override __.PropertyType = p.PropertyType
override this.SetValue(_obj, _value, _invokeAttr, _binder, _index, _culture) = notRequired this "SetValue" p.Name
override this.GetAccessors _nonPublic = notRequired this "nonPublic" p.Name
override __.GetGetMethod _nonPublic = MethodFml(tpdef,p.GetGetMethod(),parameters) :> _
override __.GetSetMethod _nonPublic = MethodFml(tpdef,p.GetSetMethod(),parameters) :> _
override __.GetIndexParameters() = p.GetIndexParameters()
override __.Attributes = p.Attributes
override __.CanRead = p.CanRead
override __.CanWrite = p.CanWrite
override this.GetValue(_obj, _invokeAttr, _binder, _index, _culture): obj = notRequired this "GetValue" p.Name
override __.Name = p.Name
override __.DeclaringType = tpdef.MakeGenericType(parameters)
override __.MemberType: MemberTypes = MemberTypes.Property
override this.ReflectedType = notRequired this "ReflectedType" p.Name
override __.GetCustomAttributes(_inherit) = emptyAttributes
override __.GetCustomAttributes(attributeType, _inherit) = Attributes.CreateEmpty attributeType
override this.IsDefined(_attributeType, _inherit) = notRequired this "IsDefined" p.Name
let pt = ProvidedTypeDefinition("TimePeriod", None)
let valueProp = typedefof<_ Option>.GetProperty("Value")
let pinfo = PropertyFml(typedefof<_ Option>, valueProp, [|pt|])
// Can now do:
// Expr.PropertyGet(myObj, pinfo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment