Created
February 22, 2014 20:45
-
-
Save lasandell/9162149 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This was the idea. Unfortunately, it doesn't work. The expression expects every successive item to be | |
// the same type the first expression, ProvidedProperty. I can upcast them all to MemberInfo, but then they | |
// don't work as arguments to the next items in the expression. | |
let createType name (parameters:obj[]) = | |
let providedAsmPath = Path.Combine(config.TemporaryFolder, "FixedLengthTypes.dll") | |
let providedAsm = ProvidedAssembly(providedAsmPath) | |
let length = parameters.[0] :?> int | |
let ty = ProvidedTypeDefinition(asm, "FixedLengthTypes", name, Some typeof<obj>, IsErased = false) | |
ty.AddMembers(list { | |
yield ProvidedProperty("Length", typeof<int>, IsStatic = true, | |
GetterCode = fun args -> <@@ length @@>) | |
let! valueField = ProvidedField("value", typeof<string>) | |
yield ProvidedProperty("Value", typeof<string>, | |
GetterCode = fun args -> Expr.FieldGet(args.[0], valueField)) | |
let! ctor = ProvidedConstructor([ProvidedParameter("value", typeof<string>)], | |
InvokeCode = fun args -> Expr.FieldSet(args.[0], valueField, args.[1])) | |
yield ProvidedMethod("Create", [ProvidedParameter("value", typeof<string>)], ty, | |
InvokeCode = fun args -> Expr.NewObject(ctor, [args.[0]])) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let createType name (parameters:obj[]) = | |
let providedAsmPath = Path.Combine(config.TemporaryFolder, "FixedLengthTypes.dll") | |
let providedAsm = ProvidedAssembly(providedAsmPath) | |
let length = parameters.[0] :?> int | |
let ty = ProvidedTypeDefinition(asm, "FixedLengthTypes", name, Some typeof<obj>, IsErased = false) | |
let lengthProp = ProvidedProperty("Length", typeof<int>, IsStatic = true, | |
GetterCode = fun args -> <@@ length @@>) | |
ty.AddMember(lengthProp) | |
let valueField = ProvidedField("value", typeof<string>) | |
ty.AddMember(valueField) | |
let valueProp = ProvidedProperty("Value", typeof<string>, | |
GetterCode = fun args -> Expr.FieldGet(args.[0], valueField)) | |
ty.AddMember(valueProp) | |
let ctor = ProvidedConstructor([ProvidedParameter("value", typeof<string>)], | |
InvokeCode = fun args -> Expr.FieldSet(args.[0], valueField, args.[1])) | |
ty.AddMember(ctor) | |
let createMeth = ProvidedMethod("Create", [ProvidedParameter("value", typeof<string>)], ty, | |
InvokeCode = fun args -> Expr.NewObject(ctor, [args.[0]])) | |
ty.AddMember(createMeth) | |
providedAsm.AddTypes([ty]) | |
ty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The list computed expression really starts to shine in this example.