5 people like it.
Like the snippet!
Type Provider for Directory
Type provider for acceess more datafile via class property.
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:
|
//Example
//open FileProvider
//let f=new FileProv<"c:/windows">("c:/windows")
//f.``win.ini``.Text
//new FileProv<complie_directory>(real_directory) // () - to current directory
//FileProv.members_from_file_names...
//.Text - Get text from file. Encoding=UTF8
//.GetText(encoding) - Get text from file. Encoding=encoding
//.StreamR - Stream for read
//.StreamW - Stream for write
//.Name - file name
//.FullName - full file name
//add to project ProvidedTypes-0.2.fs & ProvidedTypes-0.2.fsi from
//http://fsharp3sample.codeplex.com/SourceControl/latest#SampleProviders/Shared/ProvidedTypes-0.2.fs
module FileProvider
open System.IO
open System.Text
open Samples.FSharp.ProvidedTypes
open Microsoft.FSharp.Core.CompilerServices
[<TypeProvider>]
type FilePr() as this =
inherit TypeProviderForNamespaces()
let asm,ns = System.Reflection.Assembly.GetExecutingAssembly(),"FileProvider"
let IniTy = ProvidedTypeDefinition(asm, ns, "FileProv", None)
do IniTy.DefineStaticParameters([ProvidedStaticParameter("path", typeof<string>)],
fun tyName [|:? string as path|] ->
let ty = ProvidedTypeDefinition(asm, ns, tyName, None)
[ProvidedConstructor([ProvidedParameter("path",typeof<string>)],
InvokeCode=(fun [path]-> <@@ %%path:string @@>))
ProvidedConstructor([],InvokeCode=(fun _ -> <@@ Directory.GetCurrentDirectory() @@>))]
|>ty.AddMembers
Directory.GetFiles(path)|>Seq.map (fun name->FileInfo(name).Name)
|>Seq.iter (fun (name)->
let sty=ProvidedTypeDefinition(name,None)
ty.AddMember sty
[ProvidedProperty("Text",typeof<string>,
GetterCode=fun [path] -> <@@ Path.Combine((%%path:obj):?>string,name)|>File.ReadAllText @@>)
ProvidedProperty("StreamR",typeof<Stream>,
GetterCode=fun [path] -> <@@ Path.Combine((%%path:obj):?>string,name)|>File.OpenRead @@>)
ProvidedProperty("StreamW",typeof<Stream>,
GetterCode=fun [path] -> <@@ Path.Combine((%%path:obj):?>string,name)|>File.OpenWrite @@>)
ProvidedProperty("Name",typeof<string>,
GetterCode=fun _ -> <@@ name @@>)
ProvidedProperty("FullName",typeof<string>,
GetterCode=fun [path] -> <@@ Path.Combine((%%path:obj):?>string,name) @@>)]
|>sty.AddMembers
ProvidedMethod("GetText",[ProvidedParameter("Encode",typeof<Encoding>)],typeof<string>,
InvokeCode=fun [path;enc] -> <@@ File.ReadAllText( Path.Combine((%%path:obj):?>string,name),(%%enc:>Encoding)) @@>)
|>sty.AddMember
let prop=ProvidedProperty(name,sty,GetterCode=fun [arg] -> <@@ (%%arg:obj):?>string @@>)
ty.AddMember prop)
ty)
this.AddNamespace(ns, [IniTy])
[<TypeProviderAssembly>]
do()
|
module FileProvider
namespace System
namespace System.IO
namespace System.Text
namespace Microsoft.FSharp
namespace Microsoft
namespace Microsoft.FSharp.Core
namespace Microsoft.FSharp.Core.CompilerServices
Multiple items
type TypeProviderAttribute =
inherit Attribute
new : unit -> TypeProviderAttribute
Full name: Microsoft.FSharp.Core.CompilerServices.TypeProviderAttribute
--------------------
new : unit -> TypeProviderAttribute
Multiple items
type FilePr =
inherit obj
new : unit -> FilePr
Full name: FileProvider.FilePr
--------------------
new : unit -> FilePr
val this : FilePr
namespace System.Reflection
type Assembly =
member CodeBase : string
member CreateInstance : typeName:string -> obj + 2 overloads
member EntryPoint : MethodInfo
member Equals : o:obj -> bool
member EscapedCodeBase : string
member Evidence : Evidence
member FullName : string
member GetCustomAttributes : inherit:bool -> obj[] + 1 overload
member GetCustomAttributesData : unit -> IList<CustomAttributeData>
member GetExportedTypes : unit -> Type[]
...
Full name: System.Reflection.Assembly
System.Reflection.Assembly.GetExecutingAssembly() : System.Reflection.Assembly
union case Option.None: Option<'T>
val typeof<'T> : System.Type
Full name: Microsoft.FSharp.Core.Operators.typeof
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
type Directory =
static member CreateDirectory : path:string -> DirectoryInfo + 1 overload
static member Delete : path:string -> unit + 1 overload
static member EnumerateDirectories : path:string -> IEnumerable<string> + 2 overloads
static member EnumerateFileSystemEntries : path:string -> IEnumerable<string> + 2 overloads
static member EnumerateFiles : path:string -> IEnumerable<string> + 2 overloads
static member Exists : path:string -> bool
static member GetAccessControl : path:string -> DirectorySecurity + 1 overload
static member GetCreationTime : path:string -> DateTime
static member GetCreationTimeUtc : path:string -> DateTime
static member GetCurrentDirectory : unit -> string
...
Full name: System.IO.Directory
Directory.GetCurrentDirectory() : string
Directory.GetFiles(path: string) : string []
Directory.GetFiles(path: string, searchPattern: string) : string []
Directory.GetFiles(path: string, searchPattern: string, searchOption: SearchOption) : string []
module Seq
from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map
Multiple items
type FileInfo =
inherit FileSystemInfo
new : fileName:string -> FileInfo
member AppendText : unit -> StreamWriter
member CopyTo : destFileName:string -> FileInfo + 1 overload
member Create : unit -> FileStream
member CreateText : unit -> StreamWriter
member Decrypt : unit -> unit
member Delete : unit -> unit
member Directory : DirectoryInfo
member DirectoryName : string
member Encrypt : unit -> unit
...
Full name: System.IO.FileInfo
--------------------
FileInfo(fileName: string) : unit
val iter : action:('T -> unit) -> source:seq<'T> -> unit
Full name: Microsoft.FSharp.Collections.Seq.iter
type Path =
static val DirectorySeparatorChar : char
static val AltDirectorySeparatorChar : char
static val VolumeSeparatorChar : char
static val InvalidPathChars : char[]
static val PathSeparator : char
static member ChangeExtension : path:string * extension:string -> string
static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
static member GetDirectoryName : path:string -> string
static member GetExtension : path:string -> string
static member GetFileName : path:string -> string
...
Full name: System.IO.Path
Path.Combine([<System.ParamArray>] paths: string []) : string
Path.Combine(path1: string, path2: string) : string
Path.Combine(path1: string, path2: string, path3: string) : string
Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
type obj = System.Object
Full name: Microsoft.FSharp.Core.obj
type File =
static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
static member AppendAllText : path:string * contents:string -> unit + 1 overload
static member AppendText : path:string -> StreamWriter
static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
static member Create : path:string -> FileStream + 3 overloads
static member CreateText : path:string -> StreamWriter
static member Decrypt : path:string -> unit
static member Delete : path:string -> unit
static member Encrypt : path:string -> unit
static member Exists : path:string -> bool
...
Full name: System.IO.File
File.ReadAllText(path: string) : string
File.ReadAllText(path: string, encoding: Encoding) : string
type Stream =
inherit MarshalByRefObject
member BeginRead : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
member BeginWrite : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
member CanRead : bool
member CanSeek : bool
member CanTimeout : bool
member CanWrite : bool
member Close : unit -> unit
member CopyTo : destination:Stream -> unit + 1 overload
member Dispose : unit -> unit
member EndRead : asyncResult:IAsyncResult -> int
...
Full name: System.IO.Stream
File.OpenRead(path: string) : FileStream
File.OpenWrite(path: string) : FileStream
type Encoding =
member BodyName : string
member Clone : unit -> obj
member CodePage : int
member DecoderFallback : DecoderFallback with get, set
member EncoderFallback : EncoderFallback with get, set
member EncodingName : string
member Equals : value:obj -> bool
member GetByteCount : chars:char[] -> int + 3 overloads
member GetBytes : chars:char[] -> byte[] + 5 overloads
member GetCharCount : bytes:byte[] -> int + 2 overloads
...
Full name: System.Text.Encoding
Multiple items
type TypeProviderAssemblyAttribute =
inherit Attribute
new : unit -> TypeProviderAssemblyAttribute
new : assemblyName:string -> TypeProviderAssemblyAttribute
member AssemblyName : string
Full name: Microsoft.FSharp.Core.CompilerServices.TypeProviderAssemblyAttribute
--------------------
new : unit -> TypeProviderAssemblyAttribute
new : assemblyName:string -> TypeProviderAssemblyAttribute
More information