Created
September 25, 2011 19:57
-
-
Save hodzanassredin/1241061 to your computer and use it in GitHub Desktop.
Sample of file system TypeProvider
This file contains 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
namespace FilesTypeProvider | |
open System.Reflection | |
open Microsoft.FSharp.Core.CompilerServices | |
open Samples.FSharpPreviewRelease2011.ProvidedTypes | |
open System.Text.RegularExpressions | |
[<TypeProvider>] | |
type public CheckedFilesProvider() as this = | |
inherit TypeProviderForNamespaces() | |
let baseTy = typeof<obj> | |
let rec addMembers (path:string) (ownerTy:ProvidedTypeDefinition) = | |
ownerTy.AddXmlDoc "A strongly typed interface to the directory '%s'" | |
let dir = new System.IO.DirectoryInfo(path) | |
let pathField = ProvidedLiteralField("Path", typeof<string>, dir.FullName); | |
pathField.AddXmlDoc "Full path to fullName" | |
ownerTy.AddMember pathField | |
for sub in dir.EnumerateDirectories() do | |
let subTy = ProvidedTypeDefinition( | |
typeName = sub.Name.Replace(' ','_'), | |
baseType = Some baseTy, | |
HideObjectMethods = true) | |
let pathField = ProvidedLiteralField("Path", typeof<string>, sub.FullName) | |
pathField.AddXmlDoc "Full path to fullName" | |
subTy.AddMember pathField | |
addMembersSafe sub.FullName subTy | |
ownerTy.AddMember subTy | |
for file in dir.EnumerateFiles() do | |
let subTy = ProvidedTypeDefinition( | |
typeName = file.Name.Replace(' ','_'), | |
baseType = Some baseTy, | |
HideObjectMethods = true) | |
let pathField = ProvidedLiteralField("Path", typeof<string>, file.FullName) | |
pathField.AddXmlDoc "Full path to fullName" | |
subTy.AddMember pathField | |
ownerTy.AddMember subTy | |
and addMembersSafe (path:string) (ownerTy:ProvidedTypeDefinition) = | |
try | |
addMembers path ownerTy | |
with | exn -> | |
() | |
let thisAssembly = Assembly.GetExecutingAssembly() | |
let rootNamespace = "FilesTypeProvider" | |
let staticParams = [ProvidedStaticParameter("path", typeof<string>)] | |
let fileTy = ProvidedTypeDefinition(thisAssembly, rootNamespace, "FileTyped", Some baseTy) | |
do fileTy.DefineStaticParameters( | |
parameters=staticParams, | |
instantiationFunction=(fun typeName parameterValues -> | |
match parameterValues with | |
| [| :? string as path |] -> | |
let ty = ProvidedTypeDefinition( | |
assembly = thisAssembly, | |
namespaceName = rootNamespace, | |
typeName = typeName, | |
baseType = Some baseTy, | |
HideObjectMethods = true) | |
addMembersSafe path ty | |
ty | |
| _ -> failwith "unexpected parameter values")) | |
do this.AddNamespace(rootNamespace, [fileTy]) | |
[<TypeProviderAssembly>] | |
do () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use:
r @"C:\Users\hodza\Documents\Visual Studio 11\Projects\FileSystemTypeProvider\FileSystemTypeProvider\bin\Debug\FileSystemTypeProvider.dll"
open FilesTypeProvider
type T = FileTyped< @"C:\Users\hodza\Documents">
printf "Dir path = %s" T.Visual_Studio_2010.Code_Snippets.Nemerle.My_Code_Snippets.Path
.